After configuring Spring Security 3.2, _csrf.token
is not bound to a request or a session object.
This is the spring security config:
<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>
The login.jsp file
<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>
And it renders the next html:
<input type="hidden" name="" value="" />
The result is 403 HTTP status:
Invalid CSRF Token 'null' was found on the request parameter '_csrf' or header 'X-CSRF-TOKEN'.
UPDATE After some debug, the request object gets out fine form DelegatingFilterProxy, but in the line 469 of CoyoteAdapter it executes request.recycle(); that erases all the attributes...
I test in Tomcat 6.0.36, 7.0.50 with JDK 1.7.
I have not understood this behavior, rather than, it would be possible if someone point me in the direction of some application sample war with Spring Security 3.2 that works with CSRF.
spring-security.xml
) with Spring 4.0.0 RELEASE (GA), Spring Security 3.2.0 RELEASE (GA) (though it is integrated with Struts 2.3.16. I did not give it a try with Spring MVC alone). It however fails, when the request is multipart for uploading files with the status 403. I'm struggling to find a solution for it. – Polonaiseweb.xml
is crucial.MultipartFilter
must be declared beforespringSecurityFilterChain
. Hope that helps. Thanks. – Polonaise