Spring boot 如何在SpringSecurity中基于LDAP和JDBC之间的用户选择实现条件认证?

Spring boot 如何在SpringSecurity中基于LDAP和JDBC之间的用户选择实现条件认证?,spring-boot,spring-security,ldap,spring-security-ldap,Spring Boot,Spring Security,Ldap,Spring Security Ldap,希望你们都做得很好,很安全。我有一个应用程序,它允许用户使用LDAP凭据或使用存储在数据库中的凭据登录。我能够分别配置jdbc和ldap验证器,但如何有条件地实现它?比方说,用户在登录页面上选择LDAP单选按钮,然后它应该触发LDAP身份验证,如果用户选择数据库身份验证,那么它应该触发jdbc身份验证。任何帮助都将不胜感激 JDBC验证器:- 公共类安全配置扩展了WebSecurity配置适配器{ @自动连线 用户详细信息服务用户详细信息服务; @凌驾 public void configure

希望你们都做得很好,很安全。我有一个应用程序,它允许用户使用LDAP凭据或使用存储在数据库中的凭据登录。我能够分别配置jdbc和ldap验证器,但如何有条件地实现它?比方说,用户在登录页面上选择LDAP单选按钮,然后它应该触发LDAP身份验证,如果用户选择数据库身份验证,那么它应该触发jdbc身份验证。任何帮助都将不胜感激

JDBC验证器:-

公共类安全配置扩展了WebSecurity配置适配器{
@自动连线
用户详细信息服务用户详细信息服务;
@凌驾
public void configure(AuthenticationManagerBuilder auth)引发异常{
auth.userDetailsService(userDetailsService);
}
@凌驾
受保护的无效配置(HttpSecurity http)引发异常{
//TODO自动生成的方法存根
http.authorizeRequests()
.antMatchers(“/admin”).hasAnyRole(“admin”)
.antMatchers(“/user”).hasAnyRole(“管理员”、“用户”)
.antMatchers(“/”).permitAll()
.and().formLogin();
}
@豆子
公共密码编码器getPasswordEncoder(){
返回NoOpPasswordEncoder.getInstance();
}
}
**LDAP验证器:-**
@启用Web安全性
公共类应用程序安全配置扩展了WebSecurity配置适配器{
Logger Logger=LoggerFactory.getLogger(this.getClass());
@自动连线
私有用户详细信息服务用户详细信息服务;
@自动连线
AuthenticationSuccessHandler AuthenticationSuccessHandler;
@自动连线
私有身份验证FailureHandler身份验证FailureHandler;
@豆子
公共身份验证FailureHandler身份验证FailureHandler(){
返回新的AuthenticationFailureHandler(){
字符串消息=”;
@凌驾
验证失败(HttpServletRequest请求、HttpServletResponse响应、,
AuthenticationException(异常)引发IOException、ServletException{
if(exception.getClass().isAssignableFrom(UsernameNotFoundException.class)){
message=“未找到用户”;
}else if(exception.getClass().isAssignableFrom(DisabledException.class)){
message=“帐户已禁用”;
}else if(exception.getClass().isAssignableFrom(BadCredentialsException.class)){
message=“坏凭证”;
}
else if(exception.getClass().isAssignableFrom(LockedException.class)){
message=“帐户已锁定”;
}
否则{
message=“内部服务器错误”;
}
response.sendRedirect(“/WisoKeyinPortal/login?error=“+message”);
}  
};
}
@豆子
公共用户详细信息服务用户详细信息服务(){
返回super.userDetailsService();
}
@豆子
公共身份验证提供程序authProvider(){
DaoAuthenticationProvider authenticationProvider=新的DaoAuthenticationProvider();
authenticationProvider.setUserDetailsService(userDetailsService);
setPasswordEncoder(新的BCryptPasswordEncoder());
返回authenticationProvider;
}
/*---------------------------从这里获取QA评论-------------------------------*/
@Bean public AuthenticationManager AuthenticationManager(){return new
ProviderManager(Arrays.asList(activeDirectoryLdapAuthenticationProvider());
}
@Bean公共身份验证提供程序
activeDirectoryLdapAuthenticationProvider(){
ActiveDirectoryLdapAuthenticationProvider提供程序=新建
ActiveDirectoryLdapAuthenticationProvider(“xyz.com”ldap://xyz.com");
provider.setConvertSubErrorCodesToExceptions(true);
provider.setUseAuthenticationRequestCredentials(true);
setSearchFilter(“sAMAccountName={1}”);返回provider;}
/*----------------------------QA评论到此结束-----------------------------*/
@凌驾
受保护的无效配置(HttpSecurity http)引发异常{
字符串[]staticResources={/css/**',“/images/**”,“/font/**”,“/scripts/**”,“};
http.csrf().disable().authorizeRequests().antMatchers(“/login**”).permitAll().antMatchers(静态资源)
.permitAll().anyRequest().authenticated()和().formLogin().loginPage(“/login”).permitAll()
.successHandler(authenticationSuccessHandler).failureHandler(authenticationFailureHandler).和()
.logout().invalidateHttpSession(true).clearAuthentication(true)
.logoutRequestMatcher(新的AntPathRequestMatcher(“/logout”).logoutSuccessUrl(“/login”).permitAll();
//.and().sessionManagement().invalidSessionUrl(“/login?error=session”);
}
@凌驾
受保护的无效配置(AuthenticationManagerBuilder authManagerBuilder)引发异常{
//authManagerBuilder.inMemoryAuthentication()。带用户(“管理员”)。密码(“密码”)。角色(“管理员”);
/*---------------------------从这里获取QA评论-------------------------------*/
authManagerBuilder.authenticationProvider(
activeDirectoryLdapAuthenticationProvider())
.userDetailsService(userDetailsService());
/*---------------------------从这里获取QA评论-------------------------------*/
}
}