【问题标题】:Regex doesn't match antMatcher URL pattern正则表达式与 antMatcher URL 模式不匹配
【发布时间】:2019-11-28 20:30:11
【问题描述】:

我试图忽略来自身份验证的 url 我尝试了多种不同的模式,但 java 似乎无法识别它们。我的配置如下所示:

  @Override
  public void configure(WebSecurity web) throws Exception {
    super.configure(web);
    web.ignoring().antMatchers(
            "/api/pies.*active=true");
  }

我想要匹配并忽略 spring 安全性的字符串是这样的: http://"someEnv"/"somePath"/api/pies?active=true

但是,无论我尝试什么通配符组合,它都不匹配。 匹配必须有 ?active=true

下面是方法def:

    @GetMapping()
    public ResponseEntity<List<PieResponseDto>> getPies(@RequestParam(name = "active") Boolean active) 

下面是类定义:


@RestController
@RequestMapping(path = "/api/pies", produces = MediaType.APPLICATION_JSON_VALUE)

【问题讨论】:

  • 就是这样。我将它切换到 regexMatchers 并且它有效!谢谢~!

标签: java regex spring-security


【解决方案1】:

使用.regexMatchers 而不是.antMatchers,然后试试这个正则表达式:

^[a-zA-Z:\/.]*pies\?active=true$

有很多用于正则表达式的在线工具。你可以试试这个:online regex tester

【讨论】:

  • 我已经尝试过正则表达式测试器并匹配了诸如您的模式之类的字符串,但是当我将它提供给 antMatchers 函数时,它没有拾取它。
  • Ups,因为它不是您在标题中写的 Regex :) 它是 AntPattern。试试这个:/**/*?active=true 或只是/**/*active=true
  • 哦,这是新的见解!这些模式不起作用,但我认为这是因为控制器收到了响应。我会做一些研究并更新问题
  • 您的原始模式有效!如果您编辑答案,您只需要使用 .regexMatchers 而不是 .antMatchers 我可以将其标记为正确!
猜你喜欢
  • 2019-10-08
  • 1970-01-01
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
  • 1970-01-01
  • 2016-12-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多