程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Spring Security:抛出LockedException而不是BadCredentialsException,为什么?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Spring Security:抛出LockedException而不是BadCredentialsException,为什么??

开发过程中遇到Spring Security:抛出LockedException而不是BadCredentialsException,为什么?的问题如何解决?下面主要结合日常开发的经验,给出你关于Spring Security:抛出LockedException而不是BadCredentialsException,为什么?的解决方法建议,希望对你解决Spring Security:抛出LockedException而不是BadCredentialsException,为什么?有所启发或帮助; @H_675_2@你问:

@H_675_2@这是因为Spring Security将首先检查该帐户是否存在并有效,然后再检查密码。

@H_675_2@更具体:在中完成AbstractUserDetailsAuthenticationProvIDer.authenticate。在 说明中,该方法以这种方式工作:

user = retrIEveUser(username, (UsernamepasswordAuthenticationToken) authentication);
...
preAuthenticationchecks.check(user);
additionalAuthenticationchecks(user, (UsernamepasswordAuthenticationToken) authentication);
...
postAuthenticationchecks.check(user);
  • retrIEveUser -加载用户
  • preAuthenticationchecks.check(user);- DefaultPreAuthenticationchecks:检查锁定…
  • additionalAuthenticationchecks -检查密码
  • postAuthenticationchecks.check(user);- DefaultPostAuthenticationchecks检查未过期的凭证
@H_675_2@好处是,preAuthenticationchecks并且postAuthenticationchecks是接口的引用,UserDetailschecker因此您可以更改它们。只需实现自己的两个UserDetailschecker,一个用于pre的Null- Implementation,一个用于检查所有内容的post:

  • !user.isaccountnonLocked()
  • !user.isEnabled()
  • !user.isaccountnonExpired()
  • !user.isCredentialsnonExpired()

解决方法

@H_675_2@对于使用spring-security框架的基本用户身份验证,我实现了spring-security DaoAuthenticationProvider

@H_675_2@当用户尝试使用正确的用户名登录时, 错误的 密码和用户的 帐户已经被锁定 ,那么我希望spring-
security身份验证模块会抛出BadCredentialsException异常,但是会抛出异常LockedException

@H_675_2@我的问题是

  1. 为什么spring-security正在处理用户进行进一步的身份验证,而凭据特别是密码不正确?
  2. 即使用户密码无效,在应用程序中显示“用户已锁定”消息也是一种好习惯吗?
  3. 我如何设法生成/捕获BadCredentialsException无效的密码和锁定的用户?
@H_675_2@任何帮助,将不胜感激。身份验证提供程序实现代码为

@Component("authenticationProvider")
public class LoginAuthenticationProvider extends DaoAuthenticationProvider {

    @Autowired
    UserDAO userDAO;

    @Autowired
    @Qualifier("userDetailsservice")
    @Override
    public void setUserDetailsservice(UserDetailsservice userDetailsservicE) {
        super.setUserDetailsservice(userDetailsservicE);
    }

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        try {
            Authentication auth = super.authenticate(authentication);
            // if reach here,means login success,else exception will be thrown

            // reset the user attempts
            userDAO.resetpasswordRetryAttempts(authentication.getName());

            return auth;
        } catch (BadCredentialsException eX) {
            // invalid login,update user attempts
            userDAO.updatepasswordRetryAttempts(authentication.getName(),PropertyUtils.getLoginAttemptsLimit());
            throw ex;
        } catch (LockedException eX) {
            // this user is locked
            throw ex;
        } catch (AccountexpiredException eX) {
            // this user is expired
            throw ex;
        } catch (Exception eX) {
            ex.printStackTrace();
            throw ex;
        }
    }

}

大佬总结

以上是大佬教程为你收集整理的Spring Security:抛出LockedException而不是BadCredentialsException,为什么?全部内容,希望文章能够帮你解决Spring Security:抛出LockedException而不是BadCredentialsException,为什么?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。