'Setup CustomAuthenticationProvider + DaoAuthenticationProvider on Spring Boot
Initially i needed just DaoAuthenticationProvider with my custom UserDetailsService. So I just implemented it with @Service like following:
public interface UserService extends UserDetailsService {
@Service
class UserServiceImpl implements UserService {
...
private final AuthenticationManager authenticationManager;
So everything worked fine without any configs. Then i decided to add one more custom Authentication Provider. Tried it several ways.
The first one just add it in config that way:
@Configuration
@EnableGlobalMethodSecurity(
securedEnabled = true,
jsr250Enabled = true,
prePostEnabled = true
)
@EnableWebSecurity
@EnableSpringHttpSession
public class ApplicationSecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
CustomAuthenticationProvider customAuthenticationProvider(){
But it replaced DaoAuthenticationProvider by custom one. But i need both.
Next attempt:
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication();
auth.authenticationProvider(customAuthenticationProvider());
}
this time spring created two providers, BUT it used him own jdbc version of UserDetailsService. not mine.
so i tried something like this:
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(new DaoAuthenticationProvider());
auth.authenticationProvider(customAuthenticationProvider());
}
... hoping spring inject right implementation of UserDetailsService. but nope - it was null.
the last attempt is - to insert UserDetailsService manually with @Autowired: for example that way:
@Autowired
UserDetailsService userDetails;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetails);
auth.jdbcAuthentication();
auth.authenticationProvider(customAuthenticationProvider());
}
but it throws exception because of cycling dependency. really UserServiceImpl needs AuthentificationManager, but it worked before, when was the only one AuthentificationProvider, why it become a problem now, when i just want to add a one more provider?
so looks like that Spring Boot works fine only for ideal cases, but when you need something extra, there is no obvious ways how to do it. or maybe i just don't know some magic annotation?
Thanks.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|