【问题标题】:How to use "contains" in antMatchers?如何在 antMatchers 中使用“包含”?
【发布时间】:2019-12-28 03:21:24
【问题描述】:

我在 ZUUL 应用程序中使用 Spring Security,我的 API 控制对我的微服务的所有访问。使用过滤器可以允许每次登录的特定路由,在过滤器里面我有对象 HttpSecurity 通过方法“.antMatchers”进行控制。

例如:

    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and().csrf().disable();
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        http.authorizeRequests()
                .antMatchers("/**/swagger-ui.html").permitAll()
                .antMatchers("/**/webjars/**").permitAll()
                .antMatchers("/**/swagger-resources/**").permitAll()
                .antMatchers("/**/csrf/**").permitAll()
                .antMatchers("/**/v2/**").permitAll()
                .antMatchers("/**/signin/**").permitAll()
                .antMatchers("/**/microservice/chatws/**").permitAll()
                .antMatchers("/**/microservice/*swagger*").permitAll()

                .antMatchers("/**/microservice/swagger-resources/**").permitAll()
                .antMatchers("/**/microservice/v2/**").permitAll()
                .antMatchers("/**/microservice/webjars/**").permitAll()
                .antMatchers("/**/microservice/csrf/**").permitAll()
                .anyRequest()
                .authenticated();

        http.apply(new JwtTokenFilterConfigurer(jwtTokenProvider));
    }

我想允许在任何语言环境中、在乞讨、中间或结尾处有“招摇”的所有路线。我在考虑类似 String 类的方法 contains,如果包含“swagger”,那么 permitAll。

【问题讨论】:

    标签: spring spring-boot spring-security


    【解决方案1】:

    是的,您始终可以通过实现RequestMatcher 来实现您自己的匹配逻辑。以下示例显示匹配包含单词“swagger”的请求 URI(不带查询参数):

    http.authorizeRequests()
           .requestMatchers(req-> req.getRequestURI().contains("swagger")).permitAll()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-05
      • 2011-02-06
      • 2021-12-11
      • 2017-02-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多