2014-02-06 55 views
3

我参考了以下有关csrf configuration的Spring Security文档。用Spring Security重命名CSRF令牌标题名称

似乎默认标题名称为CSRF令牌是:

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

看来AngularJs使用下面的头名:X-XSRF-TOKEN

X-CSRF-TOKEN

,如文档中解释

  1. 如何更改标题名称春季安全方面?
  2. 这是继续进行的最佳方式吗?
  3. 它会影响传统的非ajax表单提交的CSRF保护,特别是XSRF参数名称吗?
+0

您是否需要在Angular中做其他任何事情才能使其工作?我使用弹簧安全3.2与上述配置,仍然得到“期望的CSRF令牌未找到。您的会话过期” –

回答

4

你可以创建自己的bean实例HttpSessionCsrfTokenRepository,设置属性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> 
+0

我使用基于Java配置的spring-security 3.2,有没有一种方法可以在HttpSessionCsrfTokenRepository中设置HeaderName。我实际上最终创建了我自己的CustomSessionCsrfTokenRepository,它仍然不起作用。 –

+0

请问这有一个新的问题,我可以在答案中。 – manish

+0

你可以在这里回答,http://stackoverflow.com/questions/24022967/using-angularjs-with-springsecurity3-2-for-csrf。我最终创建了一个CustomCsrfTokenRepository来覆盖标题名称。 –