【发布时间】:2020-08-28 07:22:54
【问题描述】:
我正在使用 Spring Security 在 Spring Boot 中开发一个 Web 应用程序。
这是我用于实现 csrf 的代码
配置类....
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity security) throws Exception {
// TODO Auto-generated method stub
security.httpBasic().disable();
security.authorizeRequests()
.antMatchers("/DDR/**").permitAll()
.antMatchers("/assets/**").permitAll();
}
}
在 JSP 中添加了以下 cmets
<meta name="_csrf" content="${_csrf.token}" />
<meta name="_csrf_header" content="${_csrf.headerName}" />
在js中获取值
var token = $("meta[name='_csrf']").attr("content");
var header = $("meta[name='_csrf_header']").attr("content");
最后在ajax请求中添加了header
headers : {
"X-CSRF-TOKEN" : token
},
我们是否需要在服务器端(Java 端)进行任何其他编码来验证 CSRF?或者 Spring Security 会处理吗?提前致谢。
【问题讨论】:
标签: spring-boot spring-security csrf-token