Scala Play 会话总是空的
Scala Play session always empty
我正在尝试在 Play 中创建一个基本的网络 login/authentication 系统。基于 Play 文档,我应该通过 Play 的 Session cookie 跨请求持久保存数据;这是我的登录信息:
def login() = Action.async(parse.json) { implicit request =>
implicit val loginInfoReads: Reads[LoginInfo] = Json.reads[LoginInfo]
val newSession = request.session +
("test" -> "yep")
// @todo: add real error handling
val unauthedUser = request.body.validate(loginInfoReads)
.getOrElse(throw new Exception("Something went wrong with the login request"))
UserService.authAndGetUser(unauthedUser.email, unauthedUser.password).map { res =>
Ok(res.name).withSession(newSession)
}
我可以在 Chrome 开发工具中看到包含在响应 cookie 中的 cookie,但是当我发出后续请求以获取会话中的数据时,我得到一个空映射:
Logger.debug(request.session.data.toString)
日志:
Map()
并尝试通过 request.session.get("test") 访问 "test" 失败。
我在这里错过了什么?为什么我的会话数据不能跨请求持久保存?
谢谢
原来这不是 Scala/Play 问题,而是 Chrome 和访问本地主机时 cookie 的更普遍的问题。以下是为我修复的问题:
我正在尝试在 Play 中创建一个基本的网络 login/authentication 系统。基于 Play 文档,我应该通过 Play 的 Session cookie 跨请求持久保存数据;这是我的登录信息:
def login() = Action.async(parse.json) { implicit request =>
implicit val loginInfoReads: Reads[LoginInfo] = Json.reads[LoginInfo]
val newSession = request.session +
("test" -> "yep")
// @todo: add real error handling
val unauthedUser = request.body.validate(loginInfoReads)
.getOrElse(throw new Exception("Something went wrong with the login request"))
UserService.authAndGetUser(unauthedUser.email, unauthedUser.password).map { res =>
Ok(res.name).withSession(newSession)
}
我可以在 Chrome 开发工具中看到包含在响应 cookie 中的 cookie,但是当我发出后续请求以获取会话中的数据时,我得到一个空映射:
Logger.debug(request.session.data.toString)
日志:
Map()
并尝试通过 request.session.get("test") 访问 "test" 失败。
我在这里错过了什么?为什么我的会话数据不能跨请求持久保存?
谢谢
原来这不是 Scala/Play 问题,而是 Chrome 和访问本地主机时 cookie 的更普遍的问题。以下是为我修复的问题: