gpt4 book ai didi

java - 防止 spring security 通过下一个身份验证提供程序对具有 BadCredentialException 的用户进行身份验证

转载 作者:行者123 更新时间:2023-11-29 04:18:20 25 4
gpt4 key购买 nike

我使用多个身份验证提供程序配置 spring security:

<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name="testUser" password="TestPwd"
authorities="ROLE_USER" />
</security:user-service>
</security:authentication-provider>
<security:authentication-provider
ref="customAuthenticationProvider" />
</security:authentication-manager>

如果用户的密码在第一个提供商中无效,我想阻止通过第二个提供商对用户进行身份验证。例如,如果用户名为“testUser”的用户无法通过内存提供程序进行身份验证(因此,用户的密码不等于“TestPwd”),customAuthenticationProvider 不会再次对用户进行身份验证。

最佳答案

所以基本上,provider manager遍历所有身份验证提供程序并检查身份验证。默认情况下,如果出现 AuthenticationException 类型的任何错误,spring 会检查另一个提供程序。

但您不想与其他提供商核实。要解决此问题,您需要拥有自己的提供商管理器并覆盖 authenticate方法。

我相信除了here 之外,覆盖方法中的整个代码将保持不变。 .在这里你只需要添加break语句。

它将如何运作?

如您所述,您有两个提供程序 1) 在内存中 2) 自定义身份验证提供程序。两者都将覆盖 public Authentication authenticate(Authentication authentication) 方法,如果凭据不匹配,此方法应抛出 BadCredentialsException

因此,在遍历 providers 时(在您的自定义提供程序管理器中),您的内存提供程序 将抛出 BadCredentialsException 并且异常将捕获 here .由于您已经编写了break,循环将退出并且自定义提供者管理器将不会去寻找另一个提供者来检查身份验证。

关于java - 防止 spring security 通过下一个身份验证提供程序对具有 BadCredentialException 的用户进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50908601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com