spring-security: requestMatchers does not execute .permitAll() for not registered Bean

Describe the bug version

  • spring boot 3.0.1
  • spring security 6.0.1

not execute .permitAll() just not registered controller.

To Reproduce image requestMatchers dose not execute .permitAll(). localhost:8080/data request response is 403 forbidden. (“/data” URL is not mvcMatcher, not registered controller API)

image but, all include matcher (“/**”) execute .permitAll().

Expected behavior

    .authorizeHttpRequests(authorize -> authorize
            .requestMatchers("/data").permitAll()
    )

localhost:8080/data request expected 404 Not Found;

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 4
  • Comments: 15 (2 by maintainers)

Most upvoted comments

I found the cause.

Error page (“/error”) is created about 404 by request “/data” but, I didn’t handled error page. processed authorizeHttpRequests default denied. I solved this issue by “/error” .permitAll or .ignoring()

Debugging through this annotation @EnableWebSecurity(debug = true)

this is my mistake. thank u @marcusdacoregio It you have any advice, comment please!

I found the cause.

Error page (“/error”) is created about 404 by request “/data” but, I didn’t handled error page. processed authorizeHttpRequests default denied. I solved this issue by “/error” .permitAll or .ignoring()

Debugging through this annotation @EnableWebSecurity(debug = true)

this is my mistake. thank u @marcusdacoregio It you have any advice, comment please!

this saved me after many hours. thank you

It you have any advice, comment please!

In your dev environment, it is good to set the logging level of Spring Security to TRACE, this way you can debug faster what is happening in the filter chain. To do that change your application.properties to include:

logging.level.org.springframework.security=TRACE

Glad you figured it out!

In my case, the following worked:

.requestMatchers("/", "index.html").permitAll()

I just had to write the file’s extention 😃

Hi, @chrisegb

Did you get it to work? For me works when I add .dispatcherTypeMatchers(FORWARD, ERROR).permitAll() before .requestMatchers("/...").permitAll()

See Spring Security docs.

An example code:

        @Bean
	public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
		return http.sessionManagement((session) -> session
				.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
				.authorizeHttpRequests(reqs -> reqs
						.dispatcherTypeMatchers(FORWARD, ERROR).permitAll()
						.requestMatchers("/api/v1/test").permitAll()
						.anyRequest().authenticated())
				.formLogin(Customizer.withDefaults())
				.httpBasic(Customizer.withDefaults())
				.csrf(AbstractHttpConfigurer::disable)
				.build();
	}

	@Bean
	public WebSecurityCustomizer webSecurityCustomizer() {
		return (web) -> web.ignoring().requestMatchers("/api/v1/users");
	}

Happy New Year!

Hi @rnjsrntkd95,

is your security configuration class annotated with @EnableWebSecurity and @Configuration? If it is, can you share a minimal, reproducible sample so we simulate it on our side?