【问题标题】:Multiple antMatcher in the Spring Security WebSecurityConfigurerAdapterSpring Security WebSecurityConfigurerAdapter中的多个antMatcher
【发布时间】:2021-04-21 14:39:13
【问题描述】:

我需要根据路径将 Spring Security 配置为具有多个过滤器:

  • 对于路径/admin/**我需要使用.addFilter(getAdminAuthFilter())
  • 对于路径/api/**我需要使用.addFilter(getApiAuthFilter())

如果我写了两个antMatcher,第一个似乎被覆盖了,我的配置不起作用。

http.csrf().disable()
    .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
    .and()
    .antMatcher("/admin/**")
    .addFilter(getAdminAuthFilter())
    .authorizeRequests()//.anyRequest().authenticated()
    .and()
    .antMatcher("/api/**")
    .addFilter(getApiAuthFilter())
    .authorizeRequests().anyRequest().authenticated();

是否可以根据请求路径配置过滤器?

【问题讨论】:

    标签: spring-boot authentication spring-security


    【解决方案1】:

    也许你可以试试这个

    http
      // ...
      .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
      .and()
            .authorizeRequests().anyRequest().authenticated()
       .and()
            .antMatcher("/admin/**")
            .addFilterBefore(getAdminAuthFilter(), FilterSecurityInterceptor.class)
            .antMatcher("/api/**")
            .addFilterBefore(getApiAuthFilter(), FilterSecurityInterceptor.class)
       // ...
    

    【讨论】:

    • 我尝试过这样的事情。问题似乎是,当我添加第二个antMatcher 时,它会覆盖第一个,整个链仅用于/api/** 路径
    猜你喜欢
    • 2022-06-13
    • 2018-06-26
    • 2016-06-23
    • 2018-02-02
    • 2016-03-25
    • 2020-01-18
    • 1970-01-01
    • 2022-06-11
    • 2017-04-06
    相关资源
    最近更新 更多