I use @EnableAuthorizationServer to get an OAuth2 AuthorizationServer for my REST API. It works. Now I want to disable user accounts after 10 failed login attempts.
I tried to configure a custom AuthenticationManager, but it's not being called.
This is my AuthorizationServerConfigurerAdapter:
@Configuration
@EnableAuthorizationServer
@EnableResourceServer
public class OAuth2AuthorizationServerConfigurer extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationClientDetailsService clientDetailsService;
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()").allowFormAuthenticationForClients();
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.withClientDetails(clientDetailsService());
}
private ClientDetailsService clientDetailsService() {
return clientDetailsService;
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.tokenStore(tokenStore()).tokenEnhancer(tokenEnhancer()).authenticationManager(authenticationManager());
}
@Bean
public TokenStore tokenStore() {
return new InMemoryTokenStore();
}
@Bean
public TokenEnhancer tokenEnhancer() {
return new CustomTokenEnhancer();
}
@Bean
public AuthenticationManager authenticationManager() {
return new CustomAuthenticationManager();
}
}
And this my dummy AuthenticationManager:
@Log4j
public class CustomAuthenticationManager implements AuthenticationManager {
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
log.error("I woz called!");
authentication.setAuthenticated(true);
return authentication;
}
}
I would expect CustomAuthenticationManager.authenticate() to be called when I request a token, so I could check the credentials and lock the account accordingly, but the method is never called :-(