g3b*_*blv 4 spring spring-security 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();
}
Run Code Online (Sandbox Code Playgroud)
有什么建议可以解决这个问题吗?
您必须使用绝对模式,请参阅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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6548 次 |
| 最近记录: |