I need to add some custom logic like using a service to log my failed attempts or reset my failed attempts on successfull login. How do I do this with httpBasic ? So I need a failure handler and success handler for httpBasic().
Currently I am trying to add custom filter like this
Spring security config
@Configuration
@EnableWebSecurity
public class SpringSecurityConfig {
@Autowired
@Qualifier("customAuthenticationEntryPoint")
AuthenticationEntryPoint authEntryPoint;
@Autowired
@Qualifier("customBasicAuthFilter")
BasicAuthenticationFilter customBasicAuthFilter;
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE + 5)
public SecurityFilterChain initSetupSecurityFilterChain(HttpSecurity http) throws Exception {
http
.securityMatcher(AntPathRequestMatcher.antMatcher("/init-setup/**"))
.authorizeHttpRequests(authorizeRequests ->
authorizeRequests
.requestMatchers(HttpMethod.POST, "/init-setup/otp").permitAll()
.requestMatchers(HttpMethod.GET, "/init-setup/default-client-credentials").authenticated()
.anyRequest().authenticated()
)
.csrf(AbstractHttpConfigurer::disable)
.sessionManagement(sessionManagement ->
sessionManagement.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
// .httpBasic(basic -> basic.authenticationEntryPoint(authEntryPoint)) // custom authentication entry point
.addFilterAt(customBasicAuthFilter, BasicAuthenticationFilter.class)
.exceptionHandling(Customizer.withDefaults());
return http.build();
}
}
customBasicAuthFilter
@Component("customAuthenticationFilter")
public class CustomBasicAuthFilter extends BasicAuthenticationFilter {
@Autowired
public CustomBasicAuthFilter(AuthenticationManager authenticationManager) {
super(authenticationManager);
}
protected void onSuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response,
Authentication authResult) throws IOException {
System.out.println("SUCCESS::");
}
protected void onUnsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response,
AuthenticationException failed) throws IOException {
System.out.println("FAILS::");
}
}
The application isn’t starting and is throwing this error
Parameter 0 of constructor in com.shabodi.laas.config.CustomBasicAuthFilter required a bean of type 'org.springframework.security.authentication.AuthenticationManager' that could not be found.
Consider defining a bean of type 'org.springframework.security.authentication.AuthenticationManager' in your configuration.
New contributor
Shaun Serrao is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.