【发布时间】:2019-06-18 02:32:27
【问题描述】:
我的应用程序有一个安全配置,它通过LDAP 对用户进行身份验证。这效果很好,但现在我想添加另一个AuthenticationProvider,它对尝试进行身份验证的用户进行更多检查。所以我尝试添加一个DbAuthenticationProvider(用于测试目的)总是拒绝访问。因此,当我尝试使用我的域帐户(适用于 activeDirectoryLdapAuthenticationProvider)登录时,我无法访问该页面,因为第二个提供商未通过身份验证。
为了实现这个目标,我使用了以下代码:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Value("${ad.domain}")
private String AD_DOMAIN;
@Value("${ad.url}")
private String AD_URL;
@Autowired
UserRoleComponent userRoleComponent;
@Autowired
DbAuthenticationProvider dbAuthenticationProvider;
private final Logger logger = LoggerFactory.getLogger(WebSecurityConfig.class);
@Override
protected void configure(HttpSecurity http) throws Exception {
this.logger.info("Verify logging level");
http.authorizeRequests().anyRequest().fullyAuthenticated().and().formLogin()
.successHandler(new CustomAuthenticationSuccessHandler()).and().httpBasic().and().logout()
.logoutUrl("/logout").invalidateHttpSession(true).deleteCookies("JSESSIONID");
http.formLogin().defaultSuccessUrl("/", true);
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(activeDirectoryLdapAuthenticationProvider());
auth.authenticationProvider(dbAuthenticationProvider);
}
@Bean
public AuthenticationManager authenticationManager() {
return new ProviderManager(Arrays.asList(activeDirectoryLdapAuthenticationProvider(), dbAuthenticationProvider));
}
@Bean
public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider(AD_DOMAIN,
AD_URL);
provider.setConvertSubErrorCodesToExceptions(true);
provider.setUseAuthenticationRequestCredentials(true);
return provider;
}
}
这是我的DbAuthenticationProvider:
@Component
public class DbAuthenticationProvider implements AuthenticationProvider {
Logger logger = LoggerFactory.getLogger(DbAuthenticationProvider.class);
@Override
public Authentication authenticate(Authentication auth) throws AuthenticationException {
auth.setAuthenticated(false);
this.logger.info("Got initialized");
return auth;
}
@Override
public boolean supports(Class<?> authentication) {
return true;
}
}
遗憾的是,我能够登录(访问没有像我预期的那样被拒绝)。我错过了什么吗?
【问题讨论】:
-
只是一个提示:您可能想查找 redhats keycloak,它可以满足您的要求,并且开箱即用,并且具有出色的 spring 集成。
-
不确定我是否关注。如果其中任何一个通过请求,则身份验证提供程序将按顺序运行。因此,在您的情况下,ldap 提供程序会处理身份验证并验证请求。这不是行为吗?
-
@Veeram 我想要的行为是在此步骤中通过 ldap 和 db 进行身份验证。因此,请检查通过 ldap 提供程序和数据库提供程序进行的身份验证(如果用户在数据库中并且通过 ldap 进行身份验证,则对请求进行身份验证,如果不是 - 不要)。
标签: spring spring-boot authentication spring-security