首页 文章

使用Spring Security重命名CSRF令牌标头名称

提问于
浏览
3

我参考了以下关于csrf configuration的Spring Security文档 .

似乎csrf令牌的默认头名是: X-CSRF-TOKEN

如文档中所述:

<meta name="_csrf" content="${_csrf.token}"/>
<!-- default header name is X-CSRF-TOKEN -->
<meta name="_csrf_header" content="${_csrf.headerName}"/>

似乎AngularJs使用以下 Headers 名称: X-XSRF-TOKEN

  • 如何在Spring安全端更改 Headers 名称?

  • 这是最好的方法吗?

  • 它会影响经典非ajax表单提交的CSRF保护,特别是XSRF参数名称吗?

1 回答

  • 4

    您可以创建自己的 HttpSessionCsrfTokenRepository bean实例,在此实例上设置属性 headerName ,并将对此实例的引用传递给CSRF配置为 <security:csrf token-repository-ref="..." /> . 例如,

    <bean id="csrfTokenRepository" class="org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository">
        <property name="headerName" value="X-SECURITY" />
    </bean>
    
    <security:http>
        <security:csrf token-repository-ref="csrfTokenRepository" />
    </security:http>
    

相关问题