Given the following code when upgrading Spring Boot version from 2.x to 3.x, e.g. from Spring Boot 2.7.15 (Spring Security 5.7.10) to Spring Boot 3.0.0 (Spring Security 6.0.0):

@Configuration
public class WebSecurityConfig {
	
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    	
        http.authorizeHttpRequests().antMatchers("/signin", "/signup").permitAll()
			.antMatchers("/users/**", "/apps/**").hasAuthority("ADMIN")
			.antMatchers("/myapps/**").hasAuthority("CLIENT")
			.anyRequest().authenticated()
        ...
 
 
        return http.build();
    }
}

You will get the error saying that:

The method antMatchers(String, String) is undefined for the type AuthorizeHttpRequestsConfigurer…

To fix this error, you need to use the requestMatchers()method instead. So update the above code as follows:

http.authorizeHttpRequests().requestMatchers("/signin", "/signup").permitAll()
		.requestMatchers("/users/**", "/apps/**").hasAuthority("ADMIN")
		.requestMatchers("/myapps/**").hasAuthority("CLIENT")
		.anyRequest().authenticated()
...

If you use Spring Boot 3.1.0 or newer (Spring Security 6.1.0 or newer), you should use lambda expression like this:

http.authorizeHttpRequests(
		auth -> auth.requestMatchers("/signin", "/signup").permitAll()
		.requestMatchers("/users/**", "/apps/**").hasAuthority("ADMIN")
		.requestMatchers("/myapps/**").hasAuthority("CLIENT")
		.anyRequest().authenticated()
       )
...

That’s how to fix the error “The method antMatchers() is undefined” in Spring Security. I hope you find this post helpful.

 

Related Articles:

How to Fix WebSecurityConfigurerAdapter Deprecated

How to Fix Deprecated Methods: authorizeRequests()...

 

Spring Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment