0

我正在开发一个 Scala Play 应用程序,并且需要通过在响应的 HTTP 标头中设置参数来禁用浏览器缓存的许多控制器操作。我决定创建一个NoCache复合动作,因为我也在使用 Deadbolt-2(并且需要一个 Deadbolt-2's AuthenticatedRequest[_]),它看起来像这样:

package action

import be.objectify.deadbolt.scala.AuthenticatedRequest
import play.api.http.HeaderNames
import play.api.mvc._

import scala.concurrent.Future
import scala.util.Success

case class NoCache[A](action: Action[A]) extends Action[A] with HeaderNames {
  def apply(request: AuthenticatedRequest[A]): Future[Result] = {
    action(request).andThen {
      case Success(result) => result.withHeaders(
        (CACHE_CONTROL -> "no-cache, no-store, must-revalidate"),
        (PRAGMA -> "no-cache"),
        (EXPIRES -> "0")
      )
    }
  }

  lazy val parser = action.parser
}

但是它不会编译尝试将这个动作混合到我的控制器动作实现中,例如

def link = deadbolt.SubjectPresent()() andThen NoCache() { implicit request =>

或者

def link = NoCache(deadbolt.SubjectPresent()()) { implicit request =>

但看不到如何组成它们......

4

1 回答 1

0

我找到了如何为单个操作执行此操作:

def index = NoCache {
  deadbolt.WithAuthRequest()() { implicit request =>
    Future {
      Ok(views.html.index(userService))
    }
  }
} 

但是,我仍然没有找到如何应用NoCache到整个控制器类。

于 2016-12-26T10:59:07.683 回答