多个Spring Security过滤器

时间:2018-11-10 10:56:16

标签: spring spring-boot spring-security

我有2个Spring Security /filter1配置。我想使用过滤器1过滤对路径/filter1/filter2的所有请求,但不包括@Override protected void configure(HttpSecurity http) throws Exception { http .csrf().disable() .sessionManagement() .sessionCreationPolicy(SessionCreationPolicy.STATELESS) .and() .authorizeRequests() .antMatchers("filter1/filter2/**").permitAll() .and() .antMatcher("filter1/**") .authorizeRequests() .anyRequest().authenticated() .and() .addFilterBefore(filter1, FilterSecurityInterceptor.class); } 路径。我要使用过滤器2过滤后者。如何实现呢?

过滤器1的配置:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .csrf().disable()
        .sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()
        .antMatcher("filter1/filter2/**")
        .authorizeRequests()
            .anyRequest().authenticated()
            .and()
        .addFilterBefore(filter2, FilterSecurityInterceptor.class);
}

过滤器2的配置:

--

1 个答案:

答案 0 :(得分:1)

只需编写一个配置,以应匹配的方式对网址进行排序(这里的排序很重要!)。

类似以下内容

http
  .csrf().disable()
  .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
  .and()
        .authorizeRequests().anyRequest().authenticated()
   .and()
        .antMatcher("filter1/filter2/**")
        .addFilterBefore(filter2, FilterSecurityInterceptor.class)
        .antMatcher("filter1/**")
        .addFilterBefore(filter1, FilterSecurityInterceptor.class);

应该那样做。它将与最具体的匹配,并使用该过滤器链。不确定是否还需要将.authorizeRequests().anyRequest().authenticated()移动到每个映射。