首页 > 解决方案 > 正则表达式与 antMatcher URL 模式不匹配

问题描述

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

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

我想匹配并忽略弹簧安全的字符串是这样的: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)

标签: javaregexspring-security

解决方案


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

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

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


推荐阅读