Search in sources :

Example 1 with CaptchaExpireException

use of com.dimple.common.exception.user.CaptchaExpireException in project DimpleBlog by martin-chips.

the class SysLoginService method login.

/**
 * 登录验证
 *
 * @param username 用户名
 * @param password 密码
 * @param captcha  验证码
 * @param uuid     唯一标识
 * @return 结果
 */
public String login(String username, String password, String code, String uuid) {
    String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
    String captcha = redisCacheService.getCacheObject(verifyKey);
    redisCacheService.deleteObject(verifyKey);
    if (captcha == null) {
        AsyncManager.me().execute(AsyncFactory.recordLoginLog(username, Constants.FAILED, MessageUtils.message("user.captcha.error")));
        throw new CaptchaExpireException();
    }
    if (!code.equalsIgnoreCase(captcha)) {
        AsyncManager.me().execute(AsyncFactory.recordLoginLog(username, Constants.FAILED, MessageUtils.message("user.captcha.expire")));
        throw new CaptchaException();
    }
    // 用户验证
    Authentication authentication = null;
    try {
        // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
        authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
    } catch (Exception e) {
        if (e instanceof BadCredentialsException) {
            AsyncManager.me().execute(AsyncFactory.recordLoginLog(username, Constants.FAILED, MessageUtils.message("user.password.not.match")));
            throw new UserPasswordNotMatchException();
        } else {
            AsyncManager.me().execute(AsyncFactory.recordLoginLog(username, Constants.FAILED, e.getMessage()));
            throw new CustomException(e.getMessage());
        }
    }
    AsyncManager.me().execute(AsyncFactory.recordLoginLog(username, Constants.SUCCESS, MessageUtils.message("user.login.success")));
    LoginUser loginUser = (LoginUser) authentication.getPrincipal();
    // 生成token
    return tokenService.createToken(loginUser);
}
Also used : CaptchaExpireException(com.dimple.common.exception.user.CaptchaExpireException) Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) CustomException(com.dimple.common.exception.CustomException) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) UserPasswordNotMatchException(com.dimple.common.exception.user.UserPasswordNotMatchException) LoginUser(com.dimple.framework.security.LoginUser) CaptchaException(com.dimple.common.exception.user.CaptchaException) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) CaptchaExpireException(com.dimple.common.exception.user.CaptchaExpireException) CaptchaException(com.dimple.common.exception.user.CaptchaException) CustomException(com.dimple.common.exception.CustomException) UserPasswordNotMatchException(com.dimple.common.exception.user.UserPasswordNotMatchException)

Aggregations

CustomException (com.dimple.common.exception.CustomException)1 CaptchaException (com.dimple.common.exception.user.CaptchaException)1 CaptchaExpireException (com.dimple.common.exception.user.CaptchaExpireException)1 UserPasswordNotMatchException (com.dimple.common.exception.user.UserPasswordNotMatchException)1 LoginUser (com.dimple.framework.security.LoginUser)1 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)1 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)1 Authentication (org.springframework.security.core.Authentication)1