hi I\'m trying to follow a simple example about doing a simple login form page that i found in this page http://docs.spring.io/autorepo/docs/spring-security/4.0.x/guid
From the Spring Security documentation
CSRF protection is enabled by default with Java configuration. If you would like to disable CSRF, the corresponding Java configuration can be seen below. Refer to the Javadoc of csrf() for additional customizations in how CSRF protection is configured.
And, when CSRF protection is enabled
The last step is to ensure that you include the CSRF token in all PATCH, POST, PUT, and DELETE methods.
In your case:
You have already determined the possible solutions:
http.csrf().disable()
; orSince you are using Thymeleaf, you will have to do something like the following in your HTML template for the login page:
Note that you must use th:action
and not HTML action
as the Thymeleaf CSRF processor will kick-in only with the former.
You could change the form submission method to GET
just to get over the problem but that isn't recommended since the users are going to submit sensitive information in the form.
I typically create a Thymeleaf fragment that is then used in all pages with forms to generate the markup for the forms with the CSRF token included. This reduces boilerplate code across the app.
Using @EnableWebMvcSecurity
instead of @EnableWebSecurity
to enable automatic injection of CSRF token with Thymeleaf tags. Also use instead of
with Spring 3.2+ and Thymeleaf 2.1+ to force Thymeleaf to include the CSRF token as a hidden field automatically (source Spring JIRA).