Skip to content

2.7.0: SecurityFilterChain antMatcher trigger all the filters on not matching route #31511

@sysmat

Description

@sysmat

Env

  • spring:2.7.0 with spring-boot-starter-security, spring-boot-starter-web java 17
  • on url path antMatcher /v1/** I have Filter1
  • on url path antMatcher /v2/** I have Filter2

error

  • when I do http request to curl.exe localhost:8080/v2/123 I see Filter2, Filter1 are triggered
  • when I do http request to curl.exe localhost:8080/v1/43 I see Filter1, Filter2 are triggered
  • in startup I see registration of filters is ok
[  restartedMain] o.s.s.web.DefaultSecurityFilterChain     : Will secure Ant [pattern='/v1/**'] with [org.springframework.security.web.session.DisableEncodeUrlFilter@1a86246a, org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@78d19287, org.springframework.security.web.context.SecurityContextPersistenceFilter@5e7e41ae, org.springframework.security.web.header.HeaderWriterFilter@771cd726, org.springframework.security.web.csrf.CsrfFilter@66bc9b34, org.springframework.security.web.authentication.logout.LogoutFilter@23a3b549, com.example.demo.FilterV1@720f5a38, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@4b2f8dd1, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@c44f810, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@e58d6e8, org.springframework.security.web.session.SessionManagementFilter@1d31806b, org.springframework.security.web.access.ExceptionTranslationFilter@6ea95506, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@149f132f]
[  restartedMain] o.s.s.web.DefaultSecurityFilterChain     : Will secure Ant [pattern='/v2/**'] with [org.springframework.security.web.session.DisableEncodeUrlFilter@59e3b068, org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@9e1696f, org.springframework.security.web.context.SecurityContextPersistenceFilter@4d4cd988, org.springframework.security.web.header.HeaderWriterFilter@7e807510, org.springframework.security.web.csrf.CsrfFilter@751d0760, org.springframework.security.web.authentication.logout.LogoutFilter@3dfa806b, com.example.demo.FilterV2@47d84948, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@40bd3f4, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@79de1e90, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@770c39d3, org.springframework.security.web.session.SessionManagementFilter@5077e2e1, org.springframework.security.web.access.ExceptionTranslationFilter@4be05dc8, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@1d116d71]


Expected behavior

Filter is triggered only for URL segment whish is defined

Demo

@EnableWebSecurity
public class SecurityConfig {

    @Autowired
    private FilterV1 filterV1;

    @Autowired
    private FilterV2 filterV2;

    @Bean
    @Order(1)
    public SecurityFilterChain filterChainV1(HttpSecurity http) throws Exception {


        http.antMatcher("/v1/**")
            .addFilterBefore(filterV1, UsernamePasswordAuthenticationFilter.class)
            .authorizeRequests().anyRequest().authenticated()
            .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

        return http.build();
    }

    @Bean
    @Order(2)
    public SecurityFilterChain filterChainV2(HttpSecurity http) throws Exception {


        http.antMatcher("/v2/**")
            .addFilterBefore(filterV2, UsernamePasswordAuthenticationFilter.class)
            .authorizeRequests().anyRequest().authenticated()
            .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

        return http.build();
    }

}

demo.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions