Ahm*_*ssy 3 java spring spring-security
我正在使用Spring安全性库来保护我的应用程序中的REST api,现在尝试尝试允许(临时)访问所有URL,但是通过以下配置,我发现仅允许GET请求,而不允许POST请求(在这里我得到403)禁止响应),我知道下面的第一个antmatcher应该同时允许GET和POST,但实际上2个antMatchers不允许POST
有人可以告诉我我在这里缺少什么吗?
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
// first I have tried only this antMatcher
.antMatchers("/**").permitAll()
// But then it didn't allow POST even when only using the line below
.antMatchers(HttpMethod.POST, "/**").permitAll()
}
Run Code Online (Sandbox Code Playgroud)
Ahm*_*ssy 10
经过一番调查,结果证明antMatcher可以按预期工作并且允许所有URL正常运行,但是我获得POST API的响应被禁止的原因是Spring安全性正在等待这些POST请求的csrf令牌,因为CSRF Spring安全默认情况下启用保护。
因此,为了使它像这样工作,您必须在POST请求中提供csrf令牌,或者可以暂时关闭CSRF保护(但是您应该在生产之前再次启用它,因为这是严重的攻击)
示例代码:
protected void configure(HttpSecurity http) throws Exception {
http
// disabling csrf here, you should enable it before using in production
.csrf().disable
.authorizeRequests()
// this matcher is working for all GET/POST/... , any URL matching the reg expression
.antMatchers("/**").permitAll()
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1707 次 |
| 最近记录: |