2

我编写了一个 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)

我确定这与会话有关...但是什么?

4

1 回答 1

4

application.secret=changeme在您的conf/application.conf文件中添加一个。

于 2012-12-04T01:04:46.903 回答