-->

I can not import filters in playframework 2.3.0

2019-06-16 03:10发布

问题:

I use playframework 2.3.0, recently I want to add the CSRFFilter

when I import csrf in global.scala:

import play.filters.csrf._

I get an error for this:

[error] G:\testprojects\app\Global.scala:7: object filters is not a member of package play [error] import play.filters.csrf._

My plugin.sbt is

...
// The Play plugin
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.3.0")
...

I use Build.scala instead of build.sbt

lazy val root  = Project("root", base = file(".")).enablePlugins(PlayScala)
.settings(baseSettings: _*)
.settings(libraryDependencies++=appDependencies)
.settings(
  scalaVersion := "2.11.1",
  version := "1.0"

)

回答1:

According to the documentation you have to add the filters dependency to your project:

libraryDependencies += filters

The documentation is for build.sbt but I guess it should work with Build.scala too.



回答2:

Play Framework GzipFilter is working for me,

my build.sbt file

name := "GZIP"

version := "1.0-SNAPSHOT"

libraryDependencies ++= Seq(
  javaJdbc,
  javaEbean,
  cache,
  filters
)     

play.Project.playJavaSettings

steps to get play.filters package
1. play
2. update            //important
3. clean
4. eclipse
5. compile
6. run

finally it will work.... (update command is important) if IDE not detecting play.filters
do the above steps one more time
finally copy paste below code

import play.GlobalSettings;
import play.api.mvc.EssentialFilter;
import play.filters.gzip.GzipFilter;

public class Global extends GlobalSettings {
    public <T extends EssentialFilter> Class<T>[] filters() {
        return new Class[]{GzipFilter.class};
    }
}



回答3:

In Play 2.4.3, the import is:

import play.filters.cors.CORSActionBuilder

It's no longer called csrf, but cors.