I followed the Play documentation to implement the CSRF check, but when I try to submit a form I receive an Unauthorized You must be authenticated to access this page.
I created a Filters class:
Enter code here...import play.http.DefaultHttpFilters;
import play.mvc.EssentialFilter;
import play.filters.csrf.CSRFFilter;
import javax.inject.Inject;
public class Filters extends DefaultHttpFilters {
@Inject
public Filters(CSRFFilter csrfFilter) {
super(csrfFilter);
}
}
I add filters to my build.sbt and enable it on applications.conf.
So in my login form I add
@helper.form(routes.Application.authenticate(), 'class -> "form-signin" ) {
@helper.CSRF.formField
In my controller i tryed:
@AddCSRFToken
public Result login() {
return ok(login.render("Login"));
}
@RequireCSRFCheck
public Result authenticate() {
DynamicForm requestData = formFactory.form().bindFromRequest();
...
...
}
Forms without the CSRF token give the same error.
If I reload the login page I'm able to enter in my application, but when I logout I receive again the error
public Result logout() {
session().clear();
return login();
}
So, how can solve the problem? Can I've form with and without CSRF?
Thanks