播放框架Redirect.withSession(“ key”-> value)未在请求中设置映射

时间:2018-11-14 17:11:25

标签: scala playframework

Scala微服务一书中的示例代码调用了Redirect()。withSession()

  def login(name: String, password: String) = Action {
if (check(name, password))
  Redirect("/auth/index").withSession("user" -> name)
else
  BadRequest("Invalid user or password") }

但是索引方法显示请求中未设置“用户”键

def index = Action { implicit request =>

println(request.session.get("user"))
request.session.get("user") match {
  case Some(user) if isValidUser(user) => Ok(s"Welcome ${user}")
  case Some(user) => BadRequest(s"${user} is not a valid user")
  case None => BadRequest(
    "You are currently not logged in. \nPlease login by calling: \n" +
    "http://localhost:9000/auth/login/admin/1234")
} }

println()语句显示None

为什么在请求会话中没有设置“用户”键?

0 个答案:

没有答案