我正在Play上做一些简单的登录验证! 2.6框架Web应用程序,似乎没有拿起我在view.loginForm
上包含的CSRF令牌:
@import helper.CSRF
@(loginform: Form[UserLoginData])(implicit messages: MessagesProvider, request: RequestHeader)
@main() {
<div class="row">
<form method="post" class="col-md-6" id="loginform" autocomplete="on">
@CSRF.formField
<input type="text id="userName">
<!-- other inputs and submit button-->
</form>
</div>
}
然后我的conf.routes通过以下方式处理POST:
POST /login controllers.LoginRegController.attemptLogin
这是LoginRegController
中的方法:
def attemptLogin() = Action {
implicit request: Request[AnyContent] => {
loginform.bindFromRequest().fold(
formWithErrors => BadRequest(views.html.login(formWithErrors)),
successfulForm => {
// check DB for valid inputs
val userName = //retrieved from form
Ok(views.html.afterLogin.dashboardviews.dashboard())
.withSession(request.session + ("authenticated" -> "true") + ("username" -> userName))
.withHeaders(SecurityHeadersFilter
.CONTENT_SECURITY_POLICY_HEADER -> " .fonts.googleapis.com ")
}
)
}
}
一旦用户点击提交,它就会被带到框架的默认授权页面,我在我的控制台上看到它未通过CSRF检查:
[warn] p.filters.CSRF - [CSRF]检查失败,因为找不到令牌 头
我认为这个答案here中提到的类似问题是会话正在刷新,但我认为它会在请求本身中有令牌,通过测试,然后加载新会话。任何想法如何修复,或如何获取令牌并在Controller方法中“必要”插入它?