I have scenario. If the client sends the username and password then my application should use DaoAuthenticationProvider.Else if my client sent a header with "phrase" the it should use PhraseAuthenticationProvider(custom). I will point out what I did so far.
- I implemented UserDetailsService as CustomSecurityCustomerService and annotated as @Service
- I created a DaoAuthenticationProvider bean configuration as shown in below code snippet in security configuration class
public class ProjectSecurityConfigurer{
@Autowired
private AuthenticationConfiguration config;
@Autowired
PhraseAuthenticationProvider pProvider;
@Bean
ExtractionFilter getExFilter() throws Exception {
return new ExtractionFilter(config.getAuthenticationManager());
}
@Bean
SecurityFilterChain projectSecSpecs(HttpSecurity http) throws Exception {
http.authorizeHttpRequests()
.antMatchers("/myaccount").authenticated()
.antMatchers("/contact","/login").permitAll();
http.httpBasic(Customizer.withDefaults());
http.addFilterBefore(getExFilter(), BasicAuthenticationFilter.class);
http.authenticationProvider(pProvider);
return http.build();
}
// @Bean
// JdbcUserDetailsManager usersInMemory(DataSource datasource) {
// return new JdbcUserDetailsManager(datasource);
// }
@Bean
DaoAuthenticationProvider getDaoBean(CustomerSecurityService service,PasswordEncoder encoder) {
DaoAuthenticationProvider daoProvider= new DaoAuthenticationProvider();
daoProvider.setUserDetailsService(service);
daoProvider.setPasswordEncoder(encoder);
return daoProvider;
}
@Bean
PasswordEncoder encoder() {
return NoOpPasswordEncoder.getInstance();
}
}
- Implemented a PhraseAuthenticationToken which extends AbstractAuthenticationToken
- Implemented PhraseAuthenticationProvider as below
@Component
public class PhraseAuthenticationProvider implements AuthenticationProvider {
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
Authentication authenticated = new PhraseAuthenticationToken("abc@123", null,null);
return authenticated;
}
@Override
public boolean supports(Class<?> authentication) {
// TODO Auto-generated method stub
return PhraseAuthenticationToken.class.equals(authentication);
}
}
This issue I am facing is if I create the DaoAuthenticationProvider bean then its not registering my PhraseAuthenticationProvider. My PhraseAuthenticationProvider only works if comment out the DaoAuthenticationProvider bean. How can I register both my DaoAuthenticationProvider and PhraseAuthenticationProvider and make it work based on the header passed