我已经成立了认证在我的应用程序是这样,当一个用户名供应和API密钥为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,使这项工作?
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添加到地图的道具,而不是实体和用户名?
很抱歉的小白因素,我想学习Scala和在同一时间播放。 :-)
干杯
我