scalacsrfplayframework-2.6

Is there a way to prevent csrf popup form in playframework 2.6


I work with playFramework 2.6. I have a popup that contains a form. When I try to submit the form, I get the following error:

Erreur Client!403 - No CSRF token found in body.

How can I fix this error?


Solution

  • Enable CSRF folder within application.conf:

    play.filters.enabled += "play.filters.csrf.CSRFFilter"
    

    Then in your form have this:

    <form action="/submit-form-url" method="POST">
      @CSRF.formField
      //Your form body  
    </form>  
    

    In case you dont want to use the helper (@CSRF.formField)

    First import the required library: import play.filters.csrf.CSRF.

    Second, get its value within the controller method: CSRF.getToken and pass it on the views.

    Third, within the views (after the form initial line) use the hidden input to hold the token value:

    <input type="hidden" name="csrfToken" value="whatever-that-is">