I am migrating a Spring Boot application from Spring boot 2.6.6 to 3.1.2 in which most of the methods in http security are either deprecated or removed.
I googled a lot of places but I am unable to convert this specific configuration into Spring Boot 3.1.X configuration.
Below are the methods which were written using a class which is extending WebSecurityConfigurerAdapter. Note : preAuthFilter(), accessDeniedHandler() & forbiddenEntryPoint() are methods defined in same class
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/**").exceptionHandling().authenticationEntryPoint(forbiddenEntryPoint())
.and().authorizeRequests().antMatchers(
"/**/index*",
"/**/logout/",
"/**/logoutApp",
"/rest/getversion*",
"/rest/dologin/*",
"/rest/menu*",
"/rest/getScript*",
"/rest/**").permitAll().antMatchers("/rest/saveTodo*").hasRole("ADMIN")
.antMatchers("/**").denyAll()
.and()
.addFilterAt(preAuthFilter(), AbstractPreAuthenticatedProcessingFilter.class)
.exceptionHandling().accessDeniedHandler(accessDeniedHandler())
.and().csrf().disable()
.headers().disable();
}
@Override
public void configure(WebSecurity web) {
web
.ignoring()
.antMatchers("/**/*.png*","/**/*.js*","/**/*.jpg*","/**/*.svg*","/**/*.ico*",
"/**/*.css*","/**/login*","/**/*.woff*","/**/*.ttf*","/**/*.eot*");
}
I tried changing the above code to below one :
@Bean
public SecurityFilterChain springFilterChain(HttpSecurity http) throws Exception {
return http.authorizeHttpRequests(req -> req.requestMatchers(
new AntPathRequestMatcher(new AntPathRequestMatcher("/**"))))
.exceptionHandling(request -> request.authenticationEntryPoint(forbiddenEntryPoint()))
.authorizeHttpRequests(request -> request
.requestMatchers(
new AntPathRequestMatcher("/**/index*"),
new AntPathRequestMatcher("/**/logout/"),
new AntPathRequestMatcher("/**/logoutApp"),
new AntPathRequestMatcher("/rest/getversion*"),
new AntPathRequestMatcher("/rest/dologin/*"),
new AntPathRequestMatcher("/rest/menu*"),
new AntPathRequestMatcher("/rest/getScript*"),
new AntPathRequestMatcher("/rest/**"),
new AntPathRequestMatcher("/waveinventoryservice/**"))
.permitAll()
.requestMatchers(new AntPathRequestMatcher("/rest/saveTodo*")).hasRole("ADMIN")
.requestMatchers(new AntPathRequestMatcher("/**"))
.denyAll())
.addFilterAt(preAuthFilter(), AbstractPreAuthenticatedProcessingFilter.class)
.exceptionHandling(handler -> handler.accessDeniedHandler(accessDeniedHandler()))
.csrf(CsrfConfigurer::disable).headers(HeadersConfigurer::disable).build();
}
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
return (web) -> web.ignoring().requestMatchers(new AntPathRequestMatcher("/**/*.png*"),
new AntPathRequestMatcher("/**/*.js*"),
new AntPathRequestMatcher("/**/*.jpg*"),
new AntPathRequestMatcher("/**/*.svg*"),
new AntPathRequestMatcher("/**/*.ico*"),
new AntPathRequestMatcher("/**/*.css*"),
new AntPathRequestMatcher("/**/login*"),
new AntPathRequestMatcher("/**/*.woff*"),
new AntPathRequestMatcher("/**/*.ttf*"),
new AntPathRequestMatcher("/**/*.eot*"));
}
this code seems to be not working because I'm getting null in the controllers & filters on invoking the method : SecurityContextHolder.getContext().getAuthentication()
even after setting it in one api with URL "rest/dologin/{userId}"
previously with the old configuration it was working but not now, can someone please try to help me out in changing this configuration?