SpringSecurity登录授权出现This object has not been built问题解决

Corwien

一、问题

在使用SpringSecurity做登录授权时,出现了这样的异常:java.lang.IllegalStateException: This object has not been built

 /**
     * 登录验证
     *
     * @param username 用户名
     * @param password 密码
     * @param code     验证码
     * @param uuid     唯一标识
     * @return 结果
     */
    public String login(String username, String password, String code, String uuid) {
        String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
        String captcha = redisCache.getCacheObject(verifyKey);
        redisCache.deleteObject(verifyKey);
        if (captcha == null) {
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire")));
            throw new CaptchaExpireException();
        }
        if (!code.equalsIgnoreCase(captcha)) {
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error")));
            throw new CaptchaException();
        }
        // 用户验证
        Authentication authentication = null;
        try {
            // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
            /*
            authentication = authenticationManager
                    .authenticate(new UsernamePasswordAuthenticationToken(username, password));
             */

            UsernamePasswordAuthenticationToken authenObj = new UsernamePasswordAuthenticationToken(username, password);

            authentication = authenticationManager
                    .authenticate(authenObj);

        } catch (Exception e) {
            if (e instanceof BadCredentialsException) {
                AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
                throw new UserPasswordNotMatchException();
            } else {
                AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage()));
                throw new CustomException(e.getMessage());
            }
        }
        AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        // 生成token
        return tokenService.createToken(loginUser);
    }

异常调试:
image.png

image.png

二、解决

目前该bug还在调试中...

阅读 3.1k

Corwien
为者常成,行者常至!

为者常成,行者常至。

6.3k 声望
0 粉丝
0 条评论

为者常成,行者常至。

6.3k 声望
1.6k 粉丝
宣传栏