【问题标题】:How can we handle the csrf attack with spring security in spring boot我们如何在 Spring Boot 中使用 Spring Security 处理 csrf 攻击
【发布时间】: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


    【解决方案1】:

    一切似乎都是正确的。 CSRF 保护在 Java 配置中默认启用。为了确保一切正常,您可以检查您发送 csrf 标头值的每个请求。此外,您应该尝试对您的应用程序进行 csrf 攻击以进行测试。您可以创建一个虚拟应用程序来调用您的 post api 并检查虚拟应用程序中的响应。如果一切配置正确,应该返回 403。

    【讨论】:

      猜你喜欢
      • 2018-03-05
      • 2021-10-10
      • 2016-08-13
      • 2019-03-01
      • 2019-09-08
      • 2014-07-02
      • 1970-01-01
      • 2022-06-17
      • 1970-01-01
      相关资源
      最近更新 更多