【发布时间】:2020-11-21 18:07:21
【问题描述】:
我目前正在使用 Spring Security 测试 REST API。因为这只是测试,所以我禁用了 CSRF。使用下面的代码,对 /users 的 Postman 获取请求可以完美运行,但来自 Postman 的任何其他类型的请求(例如 post、delete、put)都会返回 403 FORBIDDEN 错误。实在想不通。
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.anyRequest()
.authenticated()
.and()
.httpBasic();
}
}
【问题讨论】:
-
你的调试日志告诉你?
-
@ThomasAndolf 不,我没有收到任何消息
-
你的 spring 安全调试日志会告诉你为什么你会被禁止返回,除非你是从 web 浏览器调用 a,然后检查 web 浏览器控制台 (F12) 你就会知道原因对于那里的禁止。使用 Spring 安全日志更新您的问题
-
@ThomasAndolf 我在哪里可以找到这些日志?我的控制台中没有显示任何内容。
-
我不知道你是如何运行你的应用程序的,这取决于你是否在 eclipse、intellij、netbeans、命令行等中运行。如果你不知道如何查找/阅读日志,我建议您在尝试 Spring Security 之前先了解一下,以及如何调试您的应用程序。
标签: spring spring-boot spring-security csrf