Scala 已赢得播放会话信息';不能跨请求持久化-隐式存在问题吗?
我不知道为什么,直到今天这还不是个问题,而且我已经有一段时间没有更改这个控制器中的任何代码了,但我突然似乎无法在我的应用程序中使用Play 2.6保存信息。在我的LoginController中,当用户在表单中填写其凭据时,我在POST请求中调用了此方法:Scala 已赢得播放会话信息';不能跨请求持久化-隐式存在问题吗?,scala,playframework,playframework-2.6,Scala,Playframework,Playframework 2.6,我不知道为什么,直到今天这还不是个问题,而且我已经有一段时间没有更改这个控制器中的任何代码了,但我突然似乎无法在我的应用程序中使用Play 2.6保存信息。在我的LoginController中,当用户在表单中填写其凭据时,我在POST请求中调用了此方法: def attemptLogin() = Action { implicit request: Request[AnyContent] => { loginform.bindFromRequest().fold(
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>
}
就在
Ok(…)
响应之前,但这并没有发生 你的浏览器开发工具怎么说?如果转到“网络”选项卡,是否可以在响应中看到播放会话cookie?cookie是否与后续请求一起发送?您好,谢谢!饼干就在那里,与发送的内容相符。这真的一定是暗含的。由于view.html.successLoginResponse
采用隐式响应头,因此它会获取在attemptLogin()
方法开头定义为implicit request:request[AnyContent]=>
的响应头。此时,该请求的会话没有加载已验证的
或用户名
字符串。因此,模板加载旧的请求
对象,即使加载后使用的实际会话本身可能包含这些值?如果转到“网络”选项卡,是否可以在响应中看到播放会话cookie?cookie是否与后续请求一起发送?您好,谢谢!饼干就在那里,与发送的内容相符。这真的一定是暗含的。由于view.html.successLoginResponse
采用隐式响应头,因此它会获取在attemptLogin()
方法开头定义为implicit request:request[AnyContent]=>
的响应头。此时,该请求的会话没有加载已验证的
或用户名
字符串。因此,模板加载旧的请求对象,即使加载后使用的实际会话本身可能包含这些值。
request.session + ("authenticated" -> "true")
request.session + ("username" -> userName)