Java Code Examples for cn.hutool.core.lang.Validator#isEmail()

The following examples show how to use cn.hutool.core.lang.Validator#isEmail() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FrontCommentController.java    From SENS with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void run() {
    //发送通知给对方
    if (StringUtils.equals(SensConst.OPTIONS.get(BlogPropertiesEnum.SMTP_EMAIL_ENABLE.getProp()), TrueFalseEnum.TRUE.getValue()) && StringUtils.equals(SensConst.OPTIONS.get(BlogPropertiesEnum.NEW_COMMENT_NOTICE.getProp()), TrueFalseEnum.TRUE.getValue())) {
        if (Validator.isEmail(lastComment.getCommentAuthorEmail())) {
            Map<String, Object> map = new HashMap<>(8);
            map.put("blogTitle", SensConst.OPTIONS.get(BlogPropertiesEnum.BLOG_TITLE.getProp()));
            map.put("commentAuthor", lastComment.getCommentAuthor());
            map.put("pageName", post.getPostTitle());
            if (StringUtils.equals(post.getPostType(), PostTypeEnum.POST_TYPE_POST.getValue())) {
                map.put("pageUrl", SensConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()) + "/article/" + post.getId() + "#comment-id-" + comment.getId());
            } else if (StringUtils.equals(post.getPostType(), PostTypeEnum.POST_TYPE_NOTICE.getValue())) {
                map.put("pageUrl", SensConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()) + "/notice/" + post.getId() + "#comment-id-" + comment.getId());
            } else {
                map.put("pageUrl", SensConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()) + "/p/" + post.getPostUrl() + "#comment-id-" + comment.getId());
            }
            map.put("commentContent", lastComment.getCommentContent());
            map.put("replyAuthor", comment.getCommentAuthor());
            map.put("replyContent", comment.getCommentContent());
            map.put("blogUrl", SensConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()));
            mailService.sendTemplateMail(
                    lastComment.getCommentAuthorEmail(), "您在" + SensConst.OPTIONS.get(BlogPropertiesEnum.BLOG_TITLE.getProp()) + "的评论有了新回复", map, "common/mail_template/mail_reply.ftl");
        }
    }
}
 
Example 2
Source File: UserServiceImpl.java    From SENS with GNU General Public License v3.0 6 votes vote down vote up
private void basicUserCheck(User user) {
    if (user.getUserName() == null || user.getUserEmail() == null || user.getUserDisplayName() == null) {
        throw new SensBusinessException("请输入完整信息!");
    }
    String userName = user.getUserName();
    userName = userName.trim().replaceAll(" ", "-");
    if (userName.length() < 4 || userName.length() > 20) {
        throw new SensBusinessException("用户名长度为4-20位!");
    }
    if (Strings.isNotEmpty(user.getUserPass())) {
        if (user.getUserPass().length() < 6 || user.getUserPass().length() > 20) {
            throw new SensBusinessException("用户密码为6-20位!");
        }
    }
    if (!Validator.isEmail(user.getUserEmail())) {
        throw new SensBusinessException("电子邮箱格式不合法!");
    }
    if (user.getUserDisplayName().length() < 1 || user.getUserDisplayName().length() > 20) {
        throw new SensBusinessException("昵称为2-20位");
    }
}
 
Example 3
Source File: CommentController.java    From SENS with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void run() {
    if (StringUtils.equals(SensConst.OPTIONS.get(BlogPropertiesEnum.SMTP_EMAIL_ENABLE.getProp()), TrueFalseEnum.TRUE.getValue()) && StringUtils.equals(SensConst.OPTIONS.get(BlogPropertiesEnum.COMMENT_REPLY_NOTICE.getProp()), TrueFalseEnum.TRUE.getValue())) {
        if (Validator.isEmail(lastComment.getCommentAuthorEmail())) {
            Map<String, Object> map = new HashMap<>(8);
            map.put("blogTitle", SensConst.OPTIONS.get(BlogPropertiesEnum.BLOG_TITLE.getProp()));
            map.put("commentAuthor", lastComment.getCommentAuthor());
            map.put("pageName", post.getPostTitle());
            if (StringUtils.equals(post.getPostType(), PostTypeEnum.POST_TYPE_POST.getValue())) {
                map.put("pageUrl", SensConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()) + "/article/" + post.getId() + "#comment-id-" + comment.getId());
            } else if (StringUtils.equals(post.getPostType(), PostTypeEnum.POST_TYPE_NOTICE.getValue())) {
                map.put("pageUrl", SensConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()) + "/notice/" + post.getId() + "#comment-id-" + comment.getId());
            } else {
                map.put("pageUrl", SensConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()) + "/p/" + post.getPostUrl() + "#comment-id-" + comment.getId());
            }
            map.put("commentContent", lastComment.getCommentContent());
            map.put("replyAuthor", user.getUserDisplayName());
            map.put("replyContent", commentContent);
            map.put("blogUrl", SensConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()));
            mailService.sendTemplateMail(
                    lastComment.getCommentAuthorEmail(), "您在" + SensConst.OPTIONS.get(BlogPropertiesEnum.BLOG_TITLE.getProp()) + "的评论有了新回复", map, "common/mail_template/mail_reply.ftl");
        }
    }
}
 
Example 4
Source File: CommentController.java    From stone with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void run() {
    if (StrUtil.equals(HaloConst.OPTIONS.get(BlogPropertiesEnum.SMTP_EMAIL_ENABLE.getProp()), TrueFalseEnum.TRUE.getDesc()) && StrUtil.equals(HaloConst.OPTIONS.get(BlogPropertiesEnum.COMMENT_REPLY_NOTICE.getProp()), TrueFalseEnum.TRUE.getDesc())) {
        try {
            if (status == 1 && Validator.isEmail(comment.getCommentAuthorEmail())) {
                final Map<String, Object> map = new HashMap<>(6);

                final StrBuilder pageUrl = new StrBuilder(HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()));
                if (StrUtil.equals(post.getPostType(), PostTypeEnum.POST_TYPE_POST.getDesc())) {
                    pageUrl.append("/archives/");
                } else {
                    pageUrl.append("/p/");
                }
                pageUrl.append(post.getPostUrl());
                pageUrl.append("#comment-id-");
                pageUrl.append(comment.getCommentId());

                map.put("pageUrl", pageUrl.toString());
                map.put("pageName", post.getPostTitle());
                map.put("commentContent", comment.getCommentContent());
                map.put("blogUrl", HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()));
                map.put("blogTitle", HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_TITLE.getProp()));
                map.put("author", user.getUserDisplayName());
                mailService.sendTemplateMail(
                        comment.getCommentAuthorEmail(),
                        "您在" + HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()) + "的评论已审核通过!", map, "common/mail_template/mail_passed.ftl");
            }
        } catch (Exception e) {
            log.error("Mail server not configured: {}", e.getMessage());
        }
    }
}
 
Example 5
Source File: AdminServiceImpl.java    From halo with GNU General Public License v3.0 5 votes vote down vote up
@Override
public LoginPreCheckDTO getUserEnv(@NonNull String username) {
    Assert.notNull(username, "username must not be null");

    boolean useMFA = true;
    try {
        final User user = Validator.isEmail(username) ?
            userService.getByEmailOfNonNull(username) : userService.getByUsernameOfNonNull(username);
        useMFA = MFAType.useMFA(user.getMfaType());
    } catch (NotFoundException e) {
        log.error("Failed to find user by name: " + username, e);
        eventPublisher.publishEvent(new LogEvent(this, username, LogType.LOGIN_FAILED, username));
    }
    return new LoginPreCheckDTO(useMFA);
}
 
Example 6
Source File: AdminServiceImpl.java    From halo with GNU General Public License v3.0 5 votes vote down vote up
@Override
public User authenticate(LoginParam loginParam) {
    Assert.notNull(loginParam, "Login param must not be null");

    String username = loginParam.getUsername();

    String mismatchTip = "用户名或者密码不正确";

    final User user;

    try {
        // Get user by username or email
        user = Validator.isEmail(username) ?
            userService.getByEmailOfNonNull(username) : userService.getByUsernameOfNonNull(username);
    } catch (NotFoundException e) {
        log.error("Failed to find user by name: " + username, e);
        eventPublisher.publishEvent(new LogEvent(this, loginParam.getUsername(), LogType.LOGIN_FAILED, loginParam.getUsername()));

        throw new BadRequestException(mismatchTip);
    }

    userService.mustNotExpire(user);

    if (!userService.passwordMatch(user, loginParam.getPassword())) {
        // If the password is mismatch
        eventPublisher.publishEvent(new LogEvent(this, loginParam.getUsername(), LogType.LOGIN_FAILED, loginParam.getUsername()));

        throw new BadRequestException(mismatchTip);
    }

    return user;
}
 
Example 7
Source File: FrontCommentController.java    From blog-sharon with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    //发送通知给对方
    if (StrUtil.equals(HaloConst.OPTIONS.get(BlogPropertiesEnum.SMTP_EMAIL_ENABLE.getProp()), TrueFalseEnum.TRUE.getDesc()) && StrUtil.equals(HaloConst.OPTIONS.get(BlogPropertiesEnum.NEW_COMMENT_NOTICE.getProp()), TrueFalseEnum.TRUE.getDesc())) {
        if (Validator.isEmail(lastComment.getCommentAuthorEmail())) {
            Map<String, Object> map = new HashMap<>(8);
            StrBuilder pageUrl = new StrBuilder(HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()));
            if (StrUtil.equals(post.getPostType(), PostTypeEnum.POST_TYPE_POST.getDesc())) {
                pageUrl.append("/archives/");

            } else {
                pageUrl.append("/p/");
            }
            pageUrl.append(post.getPostUrl());
            pageUrl.append("#comment-id-");
            pageUrl.append(comment.getCommentId());
            map.put("pageUrl",  pageUrl.toString());
            map.put("blogTitle", HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_TITLE.getProp()));
            map.put("commentAuthor", lastComment.getCommentAuthor());
            map.put("pageName", lastComment.getPost().getPostTitle());
            map.put("commentContent", lastComment.getCommentContent());
            map.put("replyAuthor", comment.getCommentAuthor());
            map.put("replyContent", comment.getCommentContent());
            map.put("blogUrl", HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()));
            mailService.sendTemplateMail(
                    lastComment.getCommentAuthorEmail(), "您在" + HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_TITLE.getProp()) + "的评论有了新回复", map, "common/mail_template/mail_reply.ftl");
        }
    }
}
 
Example 8
Source File: CommentController.java    From blog-sharon with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    if (StrUtil.equals(HaloConst.OPTIONS.get(BlogPropertiesEnum.SMTP_EMAIL_ENABLE.getProp()), TrueFalseEnum.TRUE.getDesc()) && StrUtil.equals(HaloConst.OPTIONS.get(BlogPropertiesEnum.COMMENT_REPLY_NOTICE.getProp()), TrueFalseEnum.TRUE.getDesc())) {
        try {
            if (status == 1 && Validator.isEmail(comment.getCommentAuthorEmail())) {
                Map<String, Object> map = new HashMap<>(6);

                StrBuilder pageUrl = new StrBuilder(HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()));
                if (StrUtil.equals(post.getPostType(), PostTypeEnum.POST_TYPE_POST.getDesc())) {
                    pageUrl.append("/archives/");
                } else {
                    pageUrl.append("/p/");
                }
                pageUrl.append(post.getPostUrl());
                pageUrl.append("#comment-id-");
                pageUrl.append(comment.getCommentId());

                map.put("pageUrl",pageUrl.toString());
                map.put("pageName", post.getPostTitle());
                map.put("commentContent", comment.getCommentContent());
                map.put("blogUrl", HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()));
                map.put("blogTitle", HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_TITLE.getProp()));
                map.put("author", user.getUserDisplayName());
                mailService.sendTemplateMail(
                        comment.getCommentAuthorEmail(),
                        "您在" + HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()) + "的评论已审核通过!", map, "common/mail_template/mail_passed.ftl");
            }
        } catch (Exception e) {
            log.error("Mail server not configured: {}", e.getMessage());
        }
    }
}
 
Example 9
Source File: CommentController.java    From blog-sharon with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    if (StrUtil.equals(HaloConst.OPTIONS.get(BlogPropertiesEnum.SMTP_EMAIL_ENABLE.getProp()), TrueFalseEnum.TRUE.getDesc()) && StrUtil.equals(HaloConst.OPTIONS.get(BlogPropertiesEnum.COMMENT_REPLY_NOTICE.getProp()), TrueFalseEnum.TRUE.getDesc())) {
        if (Validator.isEmail(lastComment.getCommentAuthorEmail())) {
            Map<String, Object> map = new HashMap<>(8);
            map.put("blogTitle", HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_TITLE.getProp()));
            map.put("commentAuthor", lastComment.getCommentAuthor());
            map.put("pageName", lastComment.getPost().getPostTitle());

            StrBuilder pageUrl = new StrBuilder(HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()));
            if (StrUtil.equals(post.getPostType(), PostTypeEnum.POST_TYPE_POST.getDesc())) {
                pageUrl.append("/archives/");
            } else {
                pageUrl.append("/p/");
            }
            pageUrl.append(post.getPostUrl());
            pageUrl.append("#comment-id-");
            pageUrl.append(comment.getCommentId());

            map.put("pageUrl",pageUrl.toString());
            map.put("commentContent", lastComment.getCommentContent());
            map.put("replyAuthor", user.getUserDisplayName());
            map.put("replyContent", commentContent);
            map.put("blogUrl", HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()));
            mailService.sendTemplateMail(
                    lastComment.getCommentAuthorEmail(), "您在" + HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()) + "的评论有了新回复", map, "common/mail_template/mail_reply.ftl");
        }
    }
}
 
Example 10
Source File: FrontCommentController.java    From stone with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void run() {
    //发送通知给对方
    if (StrUtil.equals(HaloConst.OPTIONS.get(BlogPropertiesEnum.SMTP_EMAIL_ENABLE.getProp()), TrueFalseEnum.TRUE.getDesc()) && StrUtil.equals(HaloConst.OPTIONS.get(BlogPropertiesEnum.NEW_COMMENT_NOTICE.getProp()), TrueFalseEnum.TRUE.getDesc())) {
        if (Validator.isEmail(lastComment.getCommentAuthorEmail())) {
            final Map<String, Object> map = new HashMap<>(8);
            final StrBuilder pageUrl = new StrBuilder(HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()));
            if (StrUtil.equals(post.getPostType(), PostTypeEnum.POST_TYPE_POST.getDesc())) {
                pageUrl.append("/archives/");

            } else {
                pageUrl.append("/p/");
            }
            pageUrl.append(post.getPostUrl());
            pageUrl.append("#comment-id-");
            pageUrl.append(comment.getCommentId());
            map.put("pageUrl", pageUrl.toString());
            map.put("blogTitle", HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_TITLE.getProp()));
            map.put("commentAuthor", lastComment.getCommentAuthor());
            map.put("pageName", lastComment.getPost().getPostTitle());
            map.put("commentContent", lastComment.getCommentContent());
            map.put("replyAuthor", comment.getCommentAuthor());
            map.put("replyContent", comment.getCommentContent());
            map.put("blogUrl", HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()));
            mailService.sendTemplateMail(
                    lastComment.getCommentAuthorEmail(), "您在" + HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_TITLE.getProp()) + "的评论有了新回复", map, "common/mail_template/mail_reply.ftl");
        }
    }
}
 
Example 11
Source File: CommentController.java    From stone with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void run() {
    if (StrUtil.equals(HaloConst.OPTIONS.get(BlogPropertiesEnum.SMTP_EMAIL_ENABLE.getProp()), TrueFalseEnum.TRUE.getDesc()) && StrUtil.equals(HaloConst.OPTIONS.get(BlogPropertiesEnum.COMMENT_REPLY_NOTICE.getProp()), TrueFalseEnum.TRUE.getDesc())) {
        if (Validator.isEmail(lastComment.getCommentAuthorEmail())) {
            final Map<String, Object> map = new HashMap<>(8);
            map.put("blogTitle", HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_TITLE.getProp()));
            map.put("commentAuthor", lastComment.getCommentAuthor());
            map.put("pageName", lastComment.getPost().getPostTitle());

            final StrBuilder pageUrl = new StrBuilder(HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()));
            if (StrUtil.equals(post.getPostType(), PostTypeEnum.POST_TYPE_POST.getDesc())) {
                pageUrl.append("/archives/");
            } else {
                pageUrl.append("/p/");
            }
            pageUrl.append(post.getPostUrl());
            pageUrl.append("#comment-id-");
            pageUrl.append(comment.getCommentId());

            map.put("pageUrl", pageUrl.toString());
            map.put("commentContent", lastComment.getCommentContent());
            map.put("replyAuthor", user.getUserDisplayName());
            map.put("replyContent", commentContent);
            map.put("blogUrl", HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()));
            mailService.sendTemplateMail(
                    lastComment.getCommentAuthorEmail(), "您在" + HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()) + "的评论有了新回复", map, "common/mail_template/mail_reply.ftl");
        }
    }
}
 
Example 12
Source File: FreeRealm.java    From SENS with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 认证信息(身份验证) Authentication 是用来验证用户身份
 */
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    //1.验证用户名
    User user = null;
    String account = (String) token.getPrincipal();
    if (Validator.isEmail(account)) {
        user = userService.findByEmail(account);
    } else {
        user = userService.findByUserName(account);
    }
    if (user == null) {
        //用户不存在
        log.info("第三方登录,用户不存在! 登录名:{}, 密码:{}", account,token.getCredentials());
        return null;
    }

    //2.判断账号是否被封号
    if (!Objects.equals(user.getStatus(), UserStatusEnum.NORMAL.getCode())) {
        throw new LockedAccountException(localeMessageUtil.getMessage("code.admin.login.disabled.forever"));
    }

    //3.封装authenticationInfo,准备验证密码
    SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
            user,
            user.getUserPass(),
            null,
            getName()
    );
    return authenticationInfo;
}
 
Example 13
Source File: CommentController.java    From SENS with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void run() {
    if (StringUtils.equals(SensConst.OPTIONS.get(BlogPropertiesEnum.SMTP_EMAIL_ENABLE.getProp()), TrueFalseEnum.TRUE.getValue()) && StringUtils.equals(SensConst.OPTIONS.get(BlogPropertiesEnum.COMMENT_REPLY_NOTICE.getProp()), TrueFalseEnum.TRUE.getValue())) {
        try {
            //待审核的评论变成已通过,发邮件
            if (status == 1 && Validator.isEmail(comment.getCommentAuthorEmail())) {
                Map<String, Object> map = new HashMap<>(6);
                if (StringUtils.equals(post.getPostType(), PostTypeEnum.POST_TYPE_POST.getValue())) {
                    map.put("pageUrl", SensConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()) + "/article/" + post.getId() + "#comment-id-" + comment.getId());
                } else if (StringUtils.equals(post.getPostType(), PostTypeEnum.POST_TYPE_NOTICE.getValue())) {
                    map.put("pageUrl", SensConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()) + "/notice/" + post.getId() + "#comment-id-" + comment.getId());
                } else {
                    map.put("pageUrl", SensConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()) + "/p/" + post.getPostUrl() + "#comment-id-" + comment.getId());
                }
                map.put("pageName", post.getPostTitle());
                map.put("commentContent", comment.getCommentContent());
                map.put("blogUrl", SensConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()));
                map.put("blogTitle", SensConst.OPTIONS.get(BlogPropertiesEnum.BLOG_TITLE.getProp()));
                map.put("author", user.getUserDisplayName());
                mailService.sendTemplateMail(
                        comment.getCommentAuthorEmail(),
                        "您在" + SensConst.OPTIONS.get(BlogPropertiesEnum.BLOG_TITLE.getProp()) + "的评论已审核通过!", map, "common/mail_template/mail_passed.ftl");
            }
        } catch (Exception e) {
            log.error("邮件服务器未配置:{}", e.getMessage());
        }
    }
}
 
Example 14
Source File: AdminController.java    From stone with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 验证登录信息
 *
 * @param loginName 登录名:邮箱/用户名
 * @param loginPwd  loginPwd 密码
 * @param session   session session
 *
 * @return JsonResult JsonResult
 */
@PostMapping(value = "/getLogin")
@ResponseBody
public JsonResult getLogin(@ModelAttribute("loginName") String loginName,
                           @ModelAttribute("loginPwd") String loginPwd,
                           HttpSession session) {
    //已注册账号,单用户,只有一个
    final User aUser = userService.findUser();
    //首先判断是否已经被禁用已经是否已经过了10分钟
    Date loginLast = DateUtil.date();
    if (null != aUser.getLoginLast()) {
        loginLast = aUser.getLoginLast();
    }
    final Long between = DateUtil.between(loginLast, DateUtil.date(), DateUnit.MINUTE);
    if (StrUtil.equals(aUser.getLoginEnable(), TrueFalseEnum.FALSE.getDesc()) && (between < CommonParamsEnum.TEN.getValue())) {
        return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.login.disabled"));
    }
    //验证用户名和密码
    User user = null;
    if (Validator.isEmail(loginName)) {
        user = userService.userLoginByEmail(loginName, SecureUtil.md5(loginPwd));
    } else {
        user = userService.userLoginByName(loginName, SecureUtil.md5(loginPwd));
    }
    userService.updateUserLoginLast(DateUtil.date());
    //判断User对象是否相等
    if (ObjectUtil.equal(aUser, user)) {
        session.setAttribute(HaloConst.USER_SESSION_KEY, aUser);
        //重置用户的登录状态为正常
        userService.updateUserNormal();
        logsService.save(LogsRecord.LOGIN, LogsRecord.LOGIN_SUCCESS, request);
        log.info("User {} login succeeded.", aUser.getUserDisplayName());
        return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), localeMessageUtil.getMessage("code.admin.login.success"));
    } else {
        //更新失败次数
        final Integer errorCount = userService.updateUserLoginError();
        //超过五次禁用账户
        if (errorCount >= CommonParamsEnum.FIVE.getValue()) {
            userService.updateUserLoginEnable(TrueFalseEnum.FALSE.getDesc());
        }
        logsService.save(LogsRecord.LOGIN, LogsRecord.LOGIN_ERROR + "[" + HtmlUtil.escape(loginName) + "," + HtmlUtil.escape(loginPwd) + "]", request);
        final Object[] args = {(5 - errorCount)};
        return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.login.failed", args));
    }
}
 
Example 15
Source File: UserController.java    From feiqu-opensource with Apache License 2.0 4 votes vote down vote up
@ResponseBody
@PostMapping("updateUserInfo")
public Object updateUserInfo(FqUser queryUser, HttpServletRequest request, HttpServletResponse response) {
    BaseResult baseResult = new BaseResult();
    FqUserCache currUser = webUtil.currentUser(request,response);
    if(currUser == null){
        baseResult.setResult(ResultEnum.USER_NOT_LOGIN);
        return baseResult;
    }
    if(!currUser.getId().equals(queryUser.getId())){
        baseResult.setResult(ResultEnum.USER_NOT_SAME);
        return baseResult;
    }
    if(!Validator.isEmail(queryUser.getUsername())){
        baseResult.setResult(ResultEnum.EMAIL_NOT_CORRECT);
        return baseResult;
    }
    if(StringUtils.isBlank(queryUser.getNickname().trim())){
        baseResult.setResult(ResultEnum.PARAM_NULL);
        return  baseResult;
    }
    if(StringUtils.length(queryUser.getCity()) > 20){
        baseResult.setResult(ResultEnum.PARAM_ERROR);
        baseResult.setMessage("城市不能超过20个字符");
        return  baseResult;
    }
    FqUserExample example = new FqUserExample();
    example.createCriteria().andNicknameEqualTo(queryUser.getNickname().trim()).andIdNotEqualTo(queryUser.getId());
    FqUser userDB = userService.selectFirstByExample(example);
    if (null != userDB) {
        baseResult.setResult(ResultEnum.NICKNAME_EXIST);
        return baseResult;
    }
    example.clear();
    example.createCriteria().andUsernameEqualTo(queryUser.getUsername().trim()).andIdNotEqualTo(queryUser.getId());
    userDB = userService.selectFirstByExample(example);
    if (null != userDB) {
        baseResult.setResult(ResultEnum.USERNAME_EXIST);
        return baseResult;
    }
    if(!queryUser.getUsername().equals(currUser.getUsername())){
        queryUser.setIsMailBind(YesNoEnum.NO.getValue());
    }
    userService.updateByPrimaryKeySelective(queryUser);
    FqUser fqUserNew = userService.selectByPrimaryKey(queryUser.getId());
    CacheManager.refreshUserCacheByUser(new FqUserCache(fqUserNew));
    return baseResult;
}
 
Example 16
Source File: UserController.java    From feiqu-opensource with Apache License 2.0 4 votes vote down vote up
@GetMapping("reSendEmail")
@ResponseBody
public Object reSendEmail(HttpServletRequest request, HttpServletResponse response) {
    BaseResult result = new BaseResult();
    FqUserCache currUser = webUtil.currentUser(request,response);
    if(!Validator.isEmail(currUser.getUsername())){
        result.setResult(ResultEnum.EMAIL_NOT_CORRECT);
        return result;
    }
    UserActivateExample example = new UserActivateExample();
    example.createCriteria().andUserIdEqualTo(currUser.getId());
    UserActivate userActivate = userActivateService.selectFirstByExample(example);
    String htmlContent = "";
    if(userActivate == null){
        String token = RandomUtil.randomUUID();
        userActivate = new UserActivate(currUser.getId(),token,new Date());
        userActivateService.insert(userActivate);
        htmlContent = getEmailHtml(currUser.getNickname(),token);
    }else {
        htmlContent = getEmailHtml(currUser.getNickname(),userActivate.getToken());
    }

    final MimeMessage mimeMessage = mailSender.createMimeMessage();
    try {
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "GBK");
        helper.setFrom(mailSender.getUsername());
        //邮件主题
        helper.setSubject("邮箱绑定");
        //邮件接收者的邮箱地址
        helper.setTo(currUser.getUsername());
        helper.setText(htmlContent, true);

    } catch (MessagingException e) {
        logger.error("用户{}发送邮件失败 ",currUser.getUsername());
        result.setResult(ResultEnum.FAIL);
    }

    ThreadPoolTaskExecutor executor = (ThreadPoolTaskExecutor) SpringContextUtil.getBean("threadPoolTaskExecutor");
    executor.execute(new Runnable() {
        public void run() {
            mailSender.send(mimeMessage);
        }
    });
    return result;
}
 
Example 17
Source File: AdminController.java    From blog-sharon with Apache License 2.0 4 votes vote down vote up
/**
 * 验证登录信息
 *
 * @param loginName 登录名:邮箱/用户名
 * @param loginPwd  loginPwd 密码
 * @param session   session session
 * @return JsonResult JsonResult
 */
@PostMapping(value = "/getLogin")
@ResponseBody
public JsonResult getLogin(@ModelAttribute("loginName") String loginName,
                           @ModelAttribute("loginPwd") String loginPwd,
                           HttpSession session) {
    //已注册账号,单用户,只有一个
    User aUser = userService.findUser();
    //首先判断是否已经被禁用已经是否已经过了10分钟
    Date loginLast = DateUtil.date();
    if (null != aUser.getLoginLast()) {
        loginLast = aUser.getLoginLast();
    }
    Long between = DateUtil.between(loginLast, DateUtil.date(), DateUnit.MINUTE);
    if (StrUtil.equals(aUser.getLoginEnable(), TrueFalseEnum.FALSE.getDesc()) && (between < CommonParamsEnum.TEN.getValue())) {
        return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.login.disabled"));
    }
    //验证用户名和密码
    User user = null;
    if (Validator.isEmail(loginName)) {
        user = userService.userLoginByEmail(loginName, SecureUtil.md5(loginPwd));
    } else {
        user = userService.userLoginByName(loginName, SecureUtil.md5(loginPwd));
    }
    userService.updateUserLoginLast(DateUtil.date());
    //判断User对象是否相等
    if (ObjectUtil.equal(aUser, user)) {
        session.setAttribute(HaloConst.USER_SESSION_KEY, aUser);
        //重置用户的登录状态为正常
        userService.updateUserNormal();
        logsService.save(LogsRecord.LOGIN, LogsRecord.LOGIN_SUCCESS, request);
        log.info("User {} login succeeded.", aUser.getUserDisplayName());
        return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), localeMessageUtil.getMessage("code.admin.login.success"));
    } else {
        //更新失败次数
        Integer errorCount = userService.updateUserLoginError();
        //超过五次禁用账户
        if (errorCount >= CommonParamsEnum.FIVE.getValue()) {
            userService.updateUserLoginEnable(TrueFalseEnum.FALSE.getDesc());
        }
        logsService.save(LogsRecord.LOGIN, LogsRecord.LOGIN_ERROR + "[" + HtmlUtil.escape(loginName) + "," + HtmlUtil.escape(loginPwd) + "]", request);
        Object[] args = {(5 - errorCount)};
        return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.login.failed", args));
    }
}
 
Example 18
Source File: NormalRealm.java    From SENS with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 认证信息(身份验证) Authentication 是用来验证用户身份
 */
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    log.info("认证-->MyShiroRealm.doGetAuthenticationInfo()");
    //1.验证用户名
    User user = null;
    String account = (String) token.getPrincipal();
    if (Validator.isEmail(account)) {
        user = userService.findByEmail(account);
    } else {
        user = userService.findByUserName(account);
    }
    if (user == null) {
        //用户不存在
        log.info("用户不存在! 登录名:{}, 密码:{}", account, token.getCredentials());
        return null;
    }

    //2.判断账号是否被封号
    if (!Objects.equals(user.getStatus(), UserStatusEnum.NORMAL.getCode())) {
        throw new LockedAccountException(localeMessageUtil.getMessage("code.admin.login.disabled.forever"));
    }

    //3.首先判断是否已经被禁用已经是否已经过了10分钟
    Date loginLast = DateUtil.date();
    if (null != user.getLoginLast()) {
        loginLast = user.getLoginLast();
    }
    Long between = DateUtil.between(loginLast, DateUtil.date(), DateUnit.MINUTE);
    if (StringUtils.equals(user.getLoginEnable(), TrueFalseEnum.FALSE.getValue()) && (between < CommonParamsEnum.TEN.getValue())) {
        log.info("账号已锁定! 登录名:{}, 密码:{}", account, token.getCredentials());
        throw new LockedAccountException(localeMessageUtil.getMessage("code.admin.login.disabled"));
    }
    //4.封装authenticationInfo,准备验证密码
    SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
            user, // 用户名
            user.getUserPass(), // 密码
            ByteSource.Util.bytes("sens"), // 盐
            getName() // realm name
    );
    System.out.println("realName:" + getName());
    return authenticationInfo;
}