を介してユーザーを認証するアプリケーションのセキュリティ設定がありますLDAP
。これはかなりうまくいきますが、今度AuthenticationProvider
は、認証を試みるユーザーに対してさらにチェックを行う別のものを追加したいと思います。だから私はDbAuthenticationProvider
(テスト目的で)常にアクセスを拒否することを追加しようとしました。したがって、(で機能するactiveDirectoryLdapAuthenticationProvider
)ドメインアカウントでログインしようとすると、2番目のプロバイダーが認証に失敗するため、ページにアクセスできません。
この目標を達成するために、次のコードを使用しました。
@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;
}
}
悲しいことに、ログインできました(予期したとおり、アクセスは拒否されません)。私は何かを逃したのですか?
Spring AuthenticationProvider
はリクエストの認証に複数を使用しないため、オブジェクトをサポートし、リクエストを正常に認証した最初の(内のArrayList
)AuthenticationProvider
がAuthentication
使用される唯一のものになります。あなたの場合はそれactiveDirectoryLdapAuthenticationProvider
です。
を使用する代わりにActiveDirectoryLdapAuthenticationProvider
、LDAPに委任して追加のチェックを行うカスタムAuthenticationProviderを使用できます。
CustomerAuthenticationProvider implements AuthenticationProvider{
privtae ActiveDirectoryLdapAuthenticationProvider delegate; // add additional methods to initialize delegate during your configuration
@Override
public Authentication authenticate(Authentication auth) throws
AuthenticationException {
Authentication authentication= delegate.authenticate(auth);
additionalChecks(authentication);
return auth;
}
@Override
public boolean supports(Class<?> authentication) {
return UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication);
}
public void additionalCheck(Authentication authentication){
// throw AuthenticationException when it's not allowed
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加