antMatchers 匹配路径的任意开头

2024-03-04

我有将用于身份验证的 REST 服务。身份验证端点将如下所示/api/v.1/authentication。 API 版本是一个变量,可以更改它以反映更新的版本。一个例子是/api/v.2/authentication。我喜欢有一个antMatcher可以处理这两种情况,所以我尝试了.antMatchers(HttpMethod.POST,"**/authenticate").permitAll() using **匹配端点的任何开头,但这不起作用。完整设置如下。

@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();
}

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


您必须使用绝对模式,请参阅AntPathMatcher http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/util/AntPathMatcher.html:

Note:模式和路径必须都是绝对的或必须都是相对的才能使两者匹配。因此,建议此实现的用户清理模式,以便在它们前面加上“/”前缀,因为这在使用它们的上下文中有意义。

您修改和简化的配置:

@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();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

antMatchers 匹配路径的任意开头 的相关文章

随机推荐