我编写了一个 scala 方法来进行身份验证并在会话中保存一个特定的令牌,如下所示:
def logIn = Action(parse.json) { request =>
request.body.validate.map { entity =>
Authentication.authenticate(entity.username, entity.password) match {
case "" => NotFound(RestResponses.toJson(RestResponse(NOT_FOUND, "Invalid username or password.")))
case token: String => Ok(RestResponses.toJson(RestResponse(OK, "Username %s has been succesfully logged in".format(entity.username)))).withSession(TokenKey -> token)
}
}
.recover { Result =>
BadRequest(RestResponses.toJson(RestResponse(BAD_REQUEST, "Unable to parse content type for user authentication.")))
}
}
为了测试logIn
方法,我写了一个测试方法:
"A POST request to login a user" should "return OK " in {
running(FakeApplication()) {
val node = Json.toJson(AuthenticationUser("cristi", "cristi-password"))(controllers.AuthenticationService.authUserWrites);
val result = AuthenticationService.logIn(new FakeRequest(Helpers.POST, "/", FakeHeaders(), node))
status(result) should equal(OK)
contentType(result) should be(Some("application/json"))
contentAsString(result) should include("succesfully logged in")
}
}
我只是不明白为什么会收到此错误:
[info] - should return OK *** FAILED ***
[info] play.api.PlayException: Configuration error[Missing application.secret]
[info] at play.api.libs.Crypto$$anonfun$sign$2.apply(Crypto.scala:30)
[info] at play.api.libs.Crypto$$anonfun$sign$2.apply(Crypto.scala:30)
我确定这与会话有关...但是什么?