当前位置: 首页 > 知识库问答 >
问题:

Spring Security:LockedException被抛出而不是BadCreentalsException,为什么?

容修贤
2023-03-14

使用Spring Security 4.0.2。发布

对于使用Spring Security框架的基本用户身份验证,我实现了Spring Security性DaoAuthenticationProvider

当用户尝试使用正确的用户名登录时,错误的密码和用户的帐户已被锁定,那么我预计Spring Security身份验证模块将引发BadCredentialsException,但它会引发LockedException

我的问题是

  1. 为什么spring-Security正在处理用户以进行进一步身份验证,而凭据(特别是密码)不正确?
  2. 即使用户的密码无效,在应用程序中显示“User is Locked”的消息是否是好的做法?
  3. 如何管理为无效密码和锁定用户生成/catBadCreentalsException

任何帮助都将不胜感激。身份验证提供程序实现代码为

@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;
        }
    }

}

共有1个答案

仰成天
2023-03-14

您问:

Spring Security:LockedException被抛出而不是BadCreentalsException,为什么?

这是因为Spring Security会首先检查帐户是否存在且有效,然后检查密码。

更具体地说:它是在AbstractUserDetailsAuthenticationProvider中完成的。验证。在非常简短的描述中,该方法的工作方式如下:

user = retrieveUser(username, (UsernamePasswordAuthenticationToken) authentication);
...
preAuthenticationChecks.check(user);
additionalAuthenticationChecks(user, (UsernamePasswordAuthenticationToken) authentication);
...
postAuthenticationChecks.check(user);
  • <代码>检索用户-加载用户

好的一点是,预验证检查和后验证检查都是对接口的引用,因此您可以对其进行更改。只需实现您自己的两个UserDetailsChecker,一个为空的pre实现,另一个为检查所有内容的post实现:

  • <代码>!使用者isAccountNonLocked()
 类似资料:
  • 问题内容: 使用Spring Security 4.0.2.RELEASE 对于使用spring-security框架的基本用户身份验证,我实现了spring-security 当用户尝试使用正确的用户名登录时, 错误的 密码和用户的 帐户已经被锁定 ,那么我希望spring- security身份验证模块会抛出异常,但是会抛出异常 我的问题是 为什么spring-security正在处理用户进行

  • 我正在尝试使用HttpWebRequest验证Url的存在。我发现了一些基本上这样做的示例: 但是,如果url确实损坏了,它不会返回响应,而是抛出异常。 我将代码修改为: 这似乎终于做到了我想要的。 但是我想知道,为什么请求会抛出异常,而不是返回带有NotFindstatus代码的响应?

  • 以下是异常日志: 下面是android.app.ContextImpl.GetSharedReferences异常抛出的代码 让我们假设日志信息是正确的,它应该几乎是正确的。 我的第一个问题是:在文件的第358行抛出强制转换异常ContextImpl.java什么意思?那里只有一个右括号。 我猜第358行下面的语句是异常的根本原因 因为sp被声明为SharedReferencesImpl,并且当g

  • 下面是我的代码。当我运行它时,我在线程“main”java.lang.IndexOutOfBoundsException:Index:3、Size:2中得到异常,而不是我的异常消息。谁能解释一下我做错了什么,为什么会这样?谢谢!

  • 我在服务层的spring-boot应用程序中使用了Hystrix(Camden.sr7版本),而没有回退方法。Service的方法之一如下所示: 对于这样的响应,不清楚实际上是从哪个方法抛出异常的。如果我将版本更改为brixton.sr5(以前的版本),它将返回清晰的响应: 因此Hystrix的新版本(实际上是spring-cloud-dependencies的新版本)不会抛出HystrixRun

  • 当我只运行预处理器时,输出文件包含20。 然而,据我所知,预处理器只是进行文本替换。所以这就是我认为正在发生的事情(这显然是错误的,但idky): NUM被定义为10 所以我认为输出应该是10而不是20。有什么能解释出哪里出了问题吗?