小编典典

antMatchers与路径的任何开头匹配

spring-boot

我有用于身份验证的REST服务。身份验证端点将看起来像/api/v.1/authentication。API版本是可以更改以反映更新版本的变量。一个例子是/api/v.2/authentication。我希望有一个antMatcher可以处理这两种情况的应用程序,因此我尝试.antMatchers(HttpMethod.POST,"**/authenticate").permitAll()使用**匹配端点的任何开头,但这是行不通的。下面的完整设置。

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()
        .csrf().disable()
        .authorizeRequests()
             .antMatchers(HttpMethod.POST, "**/authenticate").permitAll()
             .antMatchers(HttpMethod.GET, "**/get-public-key").permitAll()
             .and()
        .authorizeRequests()
             .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
             .anyRequest().authenticated();
}

有什么建议可以解决这个问题吗?


阅读 2279

收藏
2020-05-30

共1个答案

小编典典

您必须使用绝对模式,请参阅AntPathMatcher

注意: 模式和路径必须都是绝对的,或者都必须是相对的,才能使两者匹配。因此,建议该实现方式的用户清理模式,以便在模式使用的前缀中以“
/”作为前缀。

您修改和简化的配置:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()
        .csrf().disable()
        .authorizeRequests()
            .antMatchers(HttpMethod.POST, "/**/authenticate").permitAll()
            .antMatchers(HttpMethod.GET, "/**/get-public-key").permitAll()
            .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
            .anyRequest().authenticated();
}
2020-05-30