Spring 安全发布请求

Tai*_*han 0 spring spring-security microservices

我按照本指南https://spring.io/guides/gs/securing-web/guide为我的微服务设置 spring 安全

我只是发送基本的 POST 和 GET 请求。我可以执行 GET 请求,但是当我尝试 POST 请求时,出现 403 错误。(“未找到预期的 CSRF 令牌。您的会话是否已过期?”)我正在尝试为我的微服务设置基本身份验证

谢谢

小智 6

默认情况下启用 csrf 保护。您必须将所有页面配置为包含 _csrf 令牌才能使其工作参见此处:CSRF Spring docs

您可以随时禁用 csrf 保护。如果在代码中配置:

@EnableWebSecurity
public class WebSecurityConfig extends
WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable();
}
}
Run Code Online (Sandbox Code Playgroud)

或在 XML 中:

<http>
    <!-- ... -->
    <csrf disabled="true"/>
</http>
Run Code Online (Sandbox Code Playgroud)