播放会话信息不会在请求中持续存在 - 出现问题?

时间:2017-08-29 17:52:31

标签: scala playframework playframework-2.6

我不知道为什么,直到今天这个问题还没有解决,我暂时没有更改此控制器中的任何代码,但我似乎突然无法跨请求保存信息在我用Play 2.6制作的应用程序中。在我的LoginController中,当用户在表单中填写凭证时,我从POST请求调用此方法:

def attemptLogin() = Action {
   implicit request: Request[AnyContent] => {
    loginform.bindFromRequest().fold(
      formWithErrors => BadRequest(views.html.login(formWithErrors)),
      successfulForm => {
        val validator: DbInputValidator = ... // validation object, stores Form info
        if (validator.inputsAreValid) {
          val userName = ... // retrieves user name from Form info
          Ok(views.html.loginSuccess())
            .withSession(request.session + ("authenticated" -> "true") + ("username" -> userName))
            .withHeaders(SecurityHeadersFilter
              .CONTENT_SECURITY_POLICY_HEADER -> " .fontawesome.com .fonts.googleapis.com maps.googleapis.com")
        } else {
          BadRequest(views.html.login(loginform))
        }
      }
    )
  }
}

我加载的view.html.successLoginResponse如下:

@()(implicit request: RequestHeader)

@getAuthAndUserName = @{
    val foundUsername: String = request.session.get("username").getOrElse("no username found")
    val foundAuthenticated: String = request.session.get("authenticated").getOrElse("No authentication found")
    s"$foundUsername and $foundAuthenticated"
}

@main("Successful Login") {

    @landingnavbar("login")

    <div class="container">

                    <h1>Login Successful</h1>
                    <h2>The auth token and username are:</h2>
                    <h2 id="headerValues">@getAuthAndUserName</h2>
                    <h2>Redirecting you to the Dashboard in...</h2>
                    <h3>
                        <div id="counter">3</div>
                    </h3>


    </div>

    <!-- JS here which automatically redirects to a URL after countdown -->
    <script>...</script>
}

但是当加载时,我在视图中检测到的值是&#34;否则&#34;结果&#34;未找到用户名&#34;和&#34;未找到身份验证&#34;

很明显,返回的OK请求不存储信息,即使我确实将它们添加到了 .withSession(request.session + ("authenticated" -> "true") + ("username" -> userName))

代码中的

行。在今天之前,它会这样做并且不会将信息显示给我,而是在任何后续页面加载时显示该请求 将检索数据,我将能够获取用户名或身份验证检查。它是否在顶部抓取隐式请求并将其加载到视图中?从首次调用attemptLogin()的请求的旧版本?如果是这样,我如何让它取代新的会话数据?

我试过

request.session + ("authenticated" -> "true")
request.session + ("username" -> userName)

Ok(...)回复之前,但没有采取行动。

0 个答案:

没有答案