Несколько фильтров Spring Security - PullRequest
0 голосов
/ 10 ноября 2018

У меня есть 2 Spring Security WebSecurityConfigurerAdapter конфига. Я хочу отфильтровать все запросы к пути /filter1 с фильтром 1, за исключением пути /filter1/filter2. Последний я хочу отфильтровать с помощью фильтра 2. Как я могу добиться этого?

Конфигурация фильтра 1:

@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:

@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);
}

1 Ответ

0 голосов
/ 10 ноября 2018

Просто напишите одну конфигурацию, упорядочив URL-адреса так, как они должны совпадать (порядок важен здесь!).

Что-то вроде следующего

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() к каждому сопоставлению.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...