1
我使用動作組合進行身份驗證,並避免在每個動作中傳遞通用參數。我的問題是如何將它與BodyParser parse.json結合起來,就像下面的方法一樣?Scala Play 2:動作組合和BodyParser
def setUser() = Action(parse.json) {
implicit request =>
val id = (request.body \ "id").as[String]
val username = (request.body \ "username").as[String]
val previousURI = request.session.get("previousURI") match {
case Some(x) => x
case None => "/"
}
Ok(previousURI).withSession(
"id" -> id,
"username" -> username
)
}
使用上述方法中的控制器使用 '驗證' 的性狀:
case class User(id: Option[String], username: Option[String])
case class Context(user: User, request: Request[AnyContent])
extends WrappedRequest(request)
trait Auth {
def CheckLogin(f: Context => Result) = {
Action { request =>
request.session.get("username").map { token =>
// username? yes continue...
val id = request.session.get("id")
val username = request.session.get("username")
f(Context(new User(id, username), request))
}.getOrElse {
// no redirect to login
Results.Redirect(routes.Application.login).withSession(
"previousURI" -> request.uri
)
}
}
}
}
如果我嘗試:
DEF myMethod的()= CheckLogin(parse.json){.. 。
我得到:
type mismatch; found : play.api.mvc.BodyParser[play.api.libs.json.JsValue] required: controllers.Context => play.api.mvc.Result
謝謝!