验证播放请求的parse.json [英] parse.json of authenticated play request
问题描述
我已经设置了验证我的应用程序就是这样,总是让当一个用户名,并提供了API密钥是123:
I've set up authentication in my application like this, always allow when a username is supplied and the API-key is 123:
object Auth {
def IsAuthenticated(block: => String => Request[AnyContent] => Result) = {
Security.Authenticated(RetrieveUser, HandleUnauthorized) { user =>
Action { request =>
block(user)(request)
}
}
}
def RetrieveUser(request: RequestHeader) = {
val auth = new String(base64Decode(request.headers.get("AUTHORIZATION").get.replaceFirst("Basic", "")))
val split = auth.split(":")
val user = split(0)
val pass = split(1)
Option(user)
}
def HandleUnauthorized(request: RequestHeader) = {
Results.Forbidden
}
def APIKey(apiKey: String)(f: => String => Request[AnyContent] => Result) = IsAuthenticated { user => request =>
if(apiKey == "123")
f(user)(request)
else
Results.Forbidden
}
}
我希望再在我的控制器(TESTOUT在这种情况下)来定义一个方法,该方法使用请求作为唯一的应用程序/ JSON。现在,在我加入的认证,我会说高清TESTOUT =动作(parse.json){...},但现在,我使用的身份验证,我怎么能到混合添加parse.json,使这项工作?
I want then to define a method in my controller (testOut in this case) that uses the request as application/json only. Now, before I added authentication, I'd say "def testOut = Action(parse.json) {...}", but now that I'm using authentication, how can I add parse.json in to the mix and make this work?
def testOut = Auth.APIKey("123") { username => implicit request =>
var props:Map[String, JsValue] = Map[String, JsValue]()
request.body match {
case JsObject(fields) => { props = fields.toMap }
case _ => {} // Ok("received something else: " + request.body + '\n')
}
if(!props.contains("UUID"))
props.+("UUID" -> UniqueIdGenerator.uuid)
if (!props.contains("entity"))
props.+("entity" -> "unset")
props.+("username" -> username)
Ok(props.toString)
}
作为奖励问题,为什么只UUID添加到地图的道具,而不是实体和用户名?
As a bonus question, why is only UUID added to the props map, not entity and username?
很抱歉的小白因素,我想学习Scala和在同一时间播放。 : - )
Sorry about the noob factor, I'm trying to learn Scala and Play at the same time. :-)
干杯
聂
推荐答案
事实证明,我并不需要在所有使用bodyparser,request.body有我可以使用一个asJson功能。所以,我利用这个做到以下几点。这项工作,我可以继续我的工作,但我还是不太明白如何在这里得到一个JSON解析器的身体。学习中...; - )
It turns out that I don't need to use a bodyparser at all, request.body has a asJson function that I can use. So I exploited this to do the following. This work, and I can continue my work, but I still don't quite understand how to get a JSON body parser in here. Learning in progress… ;-)
def testOut = Auth.APIKey("123") { username => request =>
var props:Map[String, JsValue] = Map[String, JsValue]()
request.body.asJson match {
case None => {}
case Some(x) => {
x match {
case JsObject(fields) => { props = fields.toMap }
case _ => {} // Ok("received something else: " + request.body + '\n')
}
}
}
if(!props.contains("UUID"))
props += "UUID" -> toJson(UniqueIdGenerator.uuid)
if(!props.contains("entity"))
props += "entity" -> toJson("unset")
props += "should" -> toJson("appear")
props += "username" -> toJson(username)
Ok(props.toString)
}
这篇关于验证播放请求的parse.json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!