Spring Security提供了多个成功的身份验证提供者
我希望web应用程序的用户通过LDAP和其他自定义身份验证进行身份验证。这是一个用Kotlin编写的Spring引导应用程序。我已按如下方式配置AuthenticationManagerBuilderSpring Security提供了多个成功的身份验证提供者,spring,authentication,spring-security,kotlin,Spring,Authentication,Spring Security,Kotlin,我希望web应用程序的用户通过LDAP和其他自定义身份验证进行身份验证。这是一个用Kotlin编写的Spring引导应用程序。我已按如下方式配置AuthenticationManagerBuilder @Autowired lateinit var authenticationProvider: CustomAuthenticationProvider override fun configure(auth: AuthenticationManagerBuilder) { auth
@Autowired
lateinit var authenticationProvider: CustomAuthenticationProvider
override fun configure(auth: AuthenticationManagerBuilder) {
auth
.authenticationProvider(authenticationProvider)
auth
.ldapAuthentication()
.userDnPatterns("uid={0},ou=people")
.groupSearchBase("ou=groups")
.contextSource()
.url("ldap://localhost:8389/dc=example,dc=com")
.and()
.passwordCompare()
.passwordEncoder(PlaintextPasswordEncoder())
.passwordAttribute("userPassword")
}
我希望链接身份验证,以便如果CustomAuthenticationProvider成功进行身份验证(函数authentication不抛出),则继续使用LDAP身份验证提供程序进行身份验证
如前所述,如果CustomAuthenticationProvider成功进行身份验证,则不会评估LDAP身份验证(以及任何后续身份验证提供程序)。仅当CustomAuthenticationProvider抛出时,才会执行LDAP身份验证
我读过许多文章(例如),其中详细介绍了具有多个身份验证提供者,但使用OR行为而不是AND行为。有什么建议吗?也许我有一些建议。但让我们分析一下之前引擎盖下发生了什么 Spring Security()中的默认身份验证管理器实现维护一个身份验证提供者列表,第一个执行成功身份验证的提供者将停止该链,其余的将不被调用。我相信你不能改变这一点。使用
AuthenticationManagerBuilder
时,将在中添加身份验证提供程序
下图显示了正在发生的事情的高级概述:
在源代码中,如下所示(为了简洁起见,跳过了详细信息):
公共身份验证(身份验证)
抛出AuthenticationException{
CLASS这怎么可能?如果您的身份验证提供程序公开身份验证,为什么LDAP要进行身份验证,因为您已经通过身份验证。唯一的方法是,您的身份验证提供程序不公开身份验证。所以您仍然没有通过身份验证。Dude!看看java If(result!=null){/..break;}
您已经找到了答案!只需返回null表示成功,它将处理下一个!异常将中断for循环。因此,为下一个返回null,为deny抛出!
public Authentication authenticate(Authentication authentication)
throws AuthenticationException {
Class<? extends Authentication> toTest = authentication.getClass();
...
for (AuthenticationProvider provider : getProviders()) {
if (!provider.supports(toTest)) {
continue;
}
try {
result = provider.authenticate(authentication);
if (result != null) {
...
break;
}
}
...
catch (AuthenticationException e) {
lastException = e;
}
}
if (result != null) {
...
return result;
}
}