Мне нужно использовать пользовательский LdapAuthenticationProvider, только с одним незначительным изменением, чтобы выполнить аутентификацию, необходимо выполнить определенное предварительное условие.
Что я хочу в основном:
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
if (!precondition) {
throw new DisabledException("");
}
return super.authenticate(authentication);
}
Мой WebSecurityConfigurerAdapter:
@Autowired
protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(userDetailsService)
.passwordEncoder(encoder);
if (ldapSecurityConfig.isLdapEnabled()) {
auth
.ldapAuthentication()
.contextSource(ldapContextSource)
.userSearchFilter(ldapSecurityConfig.getUserSearchFilter())
.ldapAuthoritiesPopulator(ldapAuthoritiesPopulator)
.userDetailsContextMapper(userDetailsContextMapper);
}
}
Проблема в том, что строка
auth.ldapAuthentication()
создаетОбъект LdapAuthenticationProviderConfigurer и его метод построения создают экземпляр объекта LdapAuthenticationProvider:
LdapAuthenticationProvider ldapAuthenticationProvider = new LdapAuthenticationProvider(ldapAuthenticator, authoritiesPopulator);
Похоже, у меня нет контроля над тем, какой LdapAuthenticationProvider будет использоваться в конце.
В качестве обходного пути я мог бы проверить предварительное условие в моем объекте UserDetailsContextMapper и выдать исключение, если оно не выполнено, но это не оптимально, поскольку в этом случае сервер LDAP будет запрашиваться, даже если он не нужен.
Мой вопрос: как я могу заставить моего настраиваемого провайдера использоваться или есть какой-то другой "простой" способ добиться желаемого поведения?