在配置了Spring Security 3.2之后,_csrf.token
未绑定到请求或会话对象。
这是Spring Security的配置:
<http pattern="/login.jsp" security="none"/>
<http>
<intercept-url pattern="/**" access="ROLE_USER"/>
<form-login login-page="/login.jsp"
authentication-failure-url="/login.jsp?error=1"
default-target-url="/index.jsp"/>
<logout/>
<csrf />
</http>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="test" password="test" authorities="ROLE_USER/>
</user-service>
</authentication-provider>
</authentication-manager>
登录页面(login.jsp文件)
<form name="f" action="${contextPath}/j_spring_security_check" method="post" >
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
<button id="ingresarButton"
name="submit"
type="submit"
class="right"
style="margin-right: 10px;">Ingresar</button>
<span>
<label for="usuario">Usuario :</label>
<input type="text" name="j_username" id="u" class="" value=''/>
</span>
<span>
<label for="clave">Contraseña :</label>
<input type="password"
name="j_password"
id="p"
class=""
onfocus="vc_psfocus = 1;"
value="">
</span>
</form>
并且它会渲染出下一个 HTML:
<input type="hidden" name="" value="" />
结果是403 HTTP状态:
Invalid CSRF Token 'null' was found on the request parameter '_csrf' or header 'X-CSRF-TOKEN'.
更新: 经过一些调试,请求对象从DelegatingFilterProxy中得到了正确的输出,但在CoyoteAdapter的469行执行request.recycle()时,它会擦除所有属性...
我使用Tomcat 6.0.36、7.0.50和JDK 1.7进行测试。
我还没有理解这种行为,不过如果有人能指点我一个使用CSRF正常工作的Spring Security 3.2的应用程序示例war包,那将是可能的。
spring-security.xml
中有一些差异),使用Spring 4.0.0 RELEASE (GA)和Spring Security 3.2.0 RELEASE (GA)(虽然它与Struts 2.3.16集成,但我没有尝试过仅使用Spring MVC)。但是,当请求为multipart以上传文件并返回状态403时,它会失败。我正在努力寻找解决方案。 - Tinyweb.xml
中过滤器的顺序非常重要。MultipartFilter
必须在springSecurityFilterChain
之前声明。希望能帮到你。谢谢。 - Tiny