How to run httpFilters in playframework 2.4

Refresh

December 2018

Views

192 time

2

I am trying to build application on Playframework 2.4 with macwire DI, and I have problem with httpFilters from Play ! Here is an example what I'm trying to do

class ExampleFilter extends Filter {
  def apply(nextFilter: RequestHeader => Future[Result])
           (requestHeader: RequestHeader): Future[Result] = {
    nextFilter(requestHeader).map { result =>
      result.withHeaders("Example" -> "test")
    }
  }
}

class Filters(ex:ExampleFilter) extends HttpFilters {
  val filters = Seq(ex)
}

DI module

trait Module extends EhCacheComponents with BuiltInComponents {

  lazy val exampleFilter = wire[ExampleFilter]
  lazy val filters = wire[Filters]

Loader class

class Loader extends ApplicationLoader {
  def load(context: Context) = {
    new MyComponents(context).application
  }
}

class MyComponents(context: Context) extends BuiltInComponentsFromContext(context) with Module {
  lazy val router: Router = wire[Routes] withPrefix "/"
}

part of application.conf is

play.http.filters = Filters

and ExampleFilter doesn't work. I'm getting response without "Example" header

2 answers

0

Похоже , ваши Filtersнаходится в безымянном пакете ( по умолчанию). Поместите его где - то в пакете, как appfilters.Filters.

Так

package appfilters

...

class Filters(ex:ExampleFilter) extends HttpFilters {

...

а также

play.http.filters = appfilters.Filters 
1

это похоже, что я нашел решение

class MyComponents(context: Context) extends BuiltInComponentsFromContext(context) with Module {
  override lazy val httpFilters = Seq(ExampleFilter)
  lazy val router: Router = wire[Routes] withPrefix "/"
}

, Просто переопределить httpFilters из BuiltInComponents признака (как выше), а также изменить ExampleFilter объекта , и это работает