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

java Spring Security,如果登录用户被禁用,如何知道密码是否错误

酆勇
2023-03-14
**Login** in *spring security*, when user is disabled, i can't know the password is wrong or not.
please,tell me how.
[AbstractUserDetailsAuthenticationProvider][1]

在spring Security中:

AbstractUserDetailsAuthenticationProvider.authenticate(){
 // (1) check disabled, if disabled, ***throw exception***
 preAuthenticationChecks.check(user);
 // (2)check password
 additionalAuthenticationChecks(user, (UsernamePasswordAuthenticationToken) authentication);
(1)`public void check(UserDetails user) {
        if (!user.isAccountNonLocked()) {
            logger.debug("User account is locked");

            throw new LockedException(messages.getMessage("AbstractUserDetailsAuthenticationProvider.locked",
                    "User account is locked"), user);
        }

        if (!user.isEnabled()) {
            logger.debug("User account is disabled");

            throw new DisabledException(messages.getMessage("AbstractUserDetailsAuthenticationProvider.disabled",
                    "User is disabled"), user);
        }

        if (!user.isAccountNonExpired()) {
            logger.debug("User account is expired");

            throw new AccountExpiredException(messages.getMessage("AbstractUserDetailsAuthenticationProvider.expired",
                    "User account has expired"), user);
        }
    }`

(2)`protected void additionalAuthenticationChecks(UserDetails UserDetails,UsernamePasswordAuthenticationToken authentication)throws AuthenticationException{Object salt

    if (this.saltSource != null) {
        salt = this.saltSource.getSalt(userDetails);
    }

    if (authentication.getCredentials() == null) {
        logger.debug("Authentication failed: no credentials provided");

        throw new BadCredentialsException(messages.getMessage(
                "AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"), userDetails);
    }

    String presentedPassword = authentication.getCredentials().toString();

    if (!passwordEncoder.isPasswordValid(userDetails.getPassword(), presentedPassword, salt)) {
        logger.debug("Authentication failed: password does not match stored value");

        throw new BadCredentialsException(messages.getMessage(
                "AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"), userDetails);
    }
}`

}

}

共有1个答案

董宜然
2023-03-14

处理此问题的一种方法是在登录页面中添加重定向

AuthenticationException ex = ((AuthenticationException) request.getSession().getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION));
if(ex instanceof DisabledException){
    //Send redirect
}
 类似资料:
  • 问题内容: 我已经阅读了两天的信息和样本,但不确定此后是否完成了所有身份验证过程。 我怎么知道我是否已登录,例如,我将有一个带有登录或注销按钮的导航栏,下面是否有一些变量,如代码? 问题答案: 如果用户已登录,则将为中的每个请求创建一个对象,您可以检查任何中间件是否存在: 您可以为此创建简单的中间件,该中间件将检查用户是否已登录,如果没有,则将重定向到页面: 并使用它:

  • 问题内容: 每次我运行使用Flask-SQLAlchemy的应用程序时,都会收到以下警告,提示该SQLALCHEMY_TRACK_MODIFICATIONS选项将被禁用。 我试图找出此选项的作用,但是Flask-SQLAlchemy文档尚不清楚该跟踪的用途。 · 如果设置为True(默认值),Flask-SQLAlchemy将跟踪对象的修改并发出信号。这需要额外的内存,如果不需要,可以将其禁用。

  • 本文向大家介绍如何知道对象是否用JavaScript密封?,包括了如何知道对象是否用JavaScript密封?的使用技巧和注意事项,需要的朋友参考一下 Object.isSealed()是用于查找对象是否在javascript中密封的方法。此方法给出布尔输出。  如果满足以下条件,则将对象密封。 1)不可扩展。 2)其属性应不可配置。 语法 参数  -Object.isSealed()将对象作为参

  • 我正在尝试构建一个带有Amazon Cognito身份验证的Android应用程序,但不想使用默认的AuthUI登录用户。 但是,我找不到任何关于如何做到这一点的参考文献、样本、例子和博客。 我试着按照手册https://docs.aws.amazon.com/aws-mobile/latest/developerguide/tutorial-android-aws-mobile-notes-au

  • 问题内容: phpMyAdmin中的 “ root”一词是什么意思? 每当我在地址栏上书写时,都会要求我输入用户名和密码,但我不知道它们是什么。我不记得我在何时何地设置它们。从哪里可以获取我的用户名和密码来登录phpMyAdmin? 问题答案: 尝试用户名= root,密码为空。

  • 如何禁用外部IDP的用户名/密码登录? 我知道我可以使用自定义主题来隐藏超文本传输协议表单,但我想正确地这样做。 据我所知,我必须至少创建自定义First Broker登录和浏览器身份验证流程,对吗? 在第一次从IDP登录之后,我必须在Keyclope中创建用户,但不要使用密码选项。 此外,浏览器流必须更新为不显示用户名/密码表单,对吗? 有人能提供适当的例子吗? 谢啦