I am migrating my project’s spring boot version from 2.7 to 3.x and therefore I was migrating my SecurityConfiguration file. Initially, it had 2 configuration classes with order 1 and 2 respectively. I had 2 login configuration, one is normal second is overview login. normal login was successfully migrated according to spring boot 3, however, overview login I am not able to migrate. Tried several things but in vain. PFB old code and tried code:
Old:
@Configuration
@Order(1)
public static class OverviewLoginWebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
MyAuthenticationSuccessHandler myAuthenticationSuccessHandler;
@Autowired
MyAuthenticationProvider myAuthenticationProvider;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(myAuthenticationProvider);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.requestMatchers().antMatchers("overviewlogin/**")
.and().exceptionHandling().authenticationEntryPoint(restAuthenticationEntryPoint())
.and().authorizeRequests().antMatchers("/overviewlogin/**").access("isAuthenticated()")
.and().cors().and().addFilterAt(authenticationTokenProcessingFilter(),UsernamePasswordAuthenticationFilter.class).logout();
}
@Bean(name = BeanIds.AUTHENTICATION_MANAGER)
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Bean
public RestAuthenticationEntryPoint restAuthenticationEntryPoint(){
return new RestAuthenticationEntryPoint();
}
@Bean
public TokenAuthenticationFilter authenticationTokenProcessingFilter() throws Exception {
return new TokenAuthenticationFilter("/overviewlogin/**", authenticationManager(), myAuthenticationSuccessHandler);
}
@Bean
public CorsConfigurationSource corsConfigurationSource()
{
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Arrays.asList("*"));
configuration.setAllowedMethods(Arrays.asList("*"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/api/**", configuration);
return source;
}
}
Tried code (spring boot 3 compatible):
@Configuration
@Order(1)
public static class OverviewLoginWebSecurityConfiguration {
private final MytAuthenticationProvider mytAuthenticationProvider;
private final MytAuthenticationSuccessHandler myAuthenticationSuccessHandler;
public OverviewLoginWebSecurityConfiguration(MyAuthenticationProvider myAuthenticationProvider,
MyAuthenticationSuccessHandler myAuthenticationSuccessHandler) {
this.myAuthenticationProvider= myAuthenticationProvider;
this.myAuthenticationSuccessHandler= myAuthenticationSuccessHandler;
}
@Bean
public SecurityFilterChain ghostloginFilterChain(HttpSecurity http, CorsConfigurationSource source) throws Exception {
http.cors(cors -> cors.configurationSource(source));
http.exceptionHandling(httpSecurityExceptionHandlingConfigurer -> httpSecurityExceptionHandlingConfigurer.authenticationEntryPoint(restAuthenticationEntryPoint()));
http.addFilterAt(authenticationTokenProcessingFilter(), UsernamePasswordAuthenticationFilter.class);
http.authorizeHttpRequests(authorize -> {
try {
authorize.requestMatchers(new AntPathRequestMatcher("/overviewlogin/**")).access(new WebExpressionAuthorizationManager(("isAuthenticated()")));
} catch (Exception e) {
throw new RuntimeException(e);
}
});
return http.build();
}
@Bean
public RestAuthenticationEntryPoint restAuthenticationEntryPoint() {
return new RestAuthenticationEntryPoint();
}
@Bean
public TokenAuthenticationFilter authenticationTokenProcessingFilter() throws Exception {
return new TokenAuthenticationFilter("/overviewlogin/**", myLoginAuthenticationManager(myAuthenticationProvider), myAuthenticationSuccessHandler);
}
@Bean
public AuthenticationManager myLoginAuthenticationManager(MyAuthenticationProvider myAuthenticationProvider) {
return new ProviderManager(Arrays.asList(myAuthenticationProvider));
}
}
This code is not working. what am I missing and what could be the solution kindly help.