问题描述
我正在使用Play 2.2.1,并且试图编写自己的Action来处理CORS请求.我发现此,但不幸的是它没有编译.
I'm using Play 2.2.1 and I'm trying to write my own Action to deal with CORS requests. I found this but unfortunately it doesn't compile.
仅供参考,以下是(略作修改的)代码:
Just for reference here's the (slightly modified) code:
import play.api.mvc._
import scala.concurrent.ExecutionContext
case class CorsAction(action: EssentialAction) extends EssentialAction {
def apply(request: RequestHeader) = {
implicit val executionContext: ExecutionContext = play.api.libs.concurrent.Execution.defaultContext
val origin = request.headers.get("Origin").getOrElse("*")
if (request.method == "OPTIONS") {
val cors = Action { request =>
Ok("").withHeaders(
"Access-Control-Allow-Origin" -> origin,
"Access-Control-Allow-Methods" -> "GET, POST, PUT, DELETE, OPTIONS",
"Access-Control-Allow-Headers" -> "Accept, Origin, Content-type, Authorization, X-Auth-Token, " +
"X-HTTP-Method-Override, X-Json, X-Prototype-Version, X-Requested-With",
"Access-Control-Allow-Credentials" -> "true",
"Access-Control-Max-Age" -> (60 * 60 * 24 * 30).toString)
}
cors(request)
} else {
action(request).map(res =>
res.withHeaders(
"Access-Control-Allow-Origin" -> origin,
"Access-Control-Allow-Credentials" -> "true"
))
}
}
}
错误是:
Cors.scala:13: not found: value Ok
我对Scala还是陌生的人,对Play甚至更是如此!而且不知道发生了什么事.据我所知,我必须使用EssentialAction,而不仅仅是Action b/c,我想获取请求的标头.到目前为止,我发现的所有示例仅涉及Action.
I'm very new to Scala and even more so to Play! and can't figure out what's going on. As far as I know I have to use EssentialAction and not just Action b/c I want to get to the header of the request. All the examples I found so far involve only Action.
推荐答案
正如威尔所说,您缺少Results
特性.
As Will said you are missing the the Results
trait.
实现CorsAction
的一种可能更干净的方法是使用ActionBuilders
,如 http://www.playframework.com/documentation/2.2.x/ScalaActionsComposition
A probably cleaner way to implement the CorsAction
would be to use ActionBuilders
as described in http://www.playframework.com/documentation/2.2.x/ScalaActionsComposition
实现如下所示:
import play.api.mvc._
import scala.concurrent.{ExecutionContext, Future}
object CorsAction extends ActionBuilder[Request] with Results{
val MaxAge = 60 * 60 * 24 * 30
val AllowHeaders = List(
"Accept", "Origin", "Content-type", "Authorization", "X-Auth-Token",
"X-HTTP-Method-Override", "X-Json", "X-Prototype-Version", "X-Requested-With")
val AllowMethods = List("GET", "POST", "PUT", "DELETE", "OPTIONS")
val AllowCredentials = true
def cors[A](origin: String) =
Ok.withHeaders(
"Access-Control-Allow-Origin" -> origin,
"Access-Control-Allow-Methods" -> AllowMethods.mkString(", "),
"Access-Control-Allow-Headers" -> AllowHeaders.mkString(", "),
"Access-Control-Allow-Credentials" -> AllowCredentials.toString,
"Access-Control-Max-Age" -> MaxAge.toString)
def invokeBlock[A](request: Request[A], block: (Request[A]) => Future[SimpleResult]) = {
implicit val executionContext = play.api.libs.concurrent.Execution.defaultContext
val origin = request.headers.get("Origin").getOrElse("*")
if (request.method == "OPTIONS") {
Future.successful(cors(origin))
} else {
block(request).map(res =>
res.withHeaders(
"Access-Control-Allow-Origin" -> origin,
"Access-Control-Allow-Credentials" -> AllowCredentials.toString
))
}
}
}
这篇关于与Play的EssentialAction组成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!