O'Reilly logo

Play Framework Cookbook - Second Edition by Giancarlo Inductivo, Alexander Reelsen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Securing form submission

Play Framework has a CSRF filter module that developers can use to validate CSRF tokens during HTTP form submissions. This allows developers to be sure that the form was submitted with a valid session token and not tampered with in any way.

How to do it...

For Java, we need to take the following steps:

  1. Add the Play filters module as a project dependency to build.sbt:
        libraryDependencies += filters
  2. Create a Global.java object file in the app/ directory:
        import play.GlobalSettings;
        import play.api.mvc.EssentialFilter;
        import play.filters.csrf.CSRFFilter;
    
        public class Global extends GlobalSettings {
            @Override
            public <T extends EssentialFilter> Class<T>[] filters() {
                return new Class[]{CSRFFilter.class};
            }
        }
  3. Declare the Global.java ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required