当前位置:  开发笔记 > 编程语言 > 正文

未经授权:您必须通过身份验证才能访问此页面. - 通过游戏表格时

如何解决《未经授权:您必须通过身份验证才能访问此页面.-通过游戏表格时》经验,为你挑选了1个好方法。

现在我知道我可能没有干净的代码,但我只想要该死的东西.它给了我一个"未经授权.您必须通过身份验证才能访问此页面"错误.

我有以下控制器

    package controllers

import javax.inject._
import play.api._
import play.api.mvc._
import play.api.data._
import play.api.data.Forms._
import models.UserData
import models.Contact
import play.api.i18n._
/**
 * This controller creates an `Action` to handle HTTP requests to the
 * application's home page.
 */
@Singleton
class HomeController @Inject()(val messagesApi: MessagesApi)extends Controller with I18nSupport {

  /**
   * Create an Action to render an HTML page.
   *
   * The configuration in the `routes` file means that this method
   * will be called when the application receives a `GET` request with
   * a path of `/`.
   */
  def index = Action { implicit request =>
     Ok("Got request [" + request + "]")
  }

  val userForm = Form(mapping("name"->nonEmptyText, "age"->number(min=0, max=100))(UserData.apply)(UserData.unapply))

  def userPost = Action {
    implicit request =>
      userForm.bindFromRequest.fold(
          formWithErrors => {
            BadRequest(views.html.user(formWithErrors))
          },
          userData => {
            val newUser = models.UserData(userData.name, userData.age)
            Redirect(routes.HomeController.home())
          })
  }

  def home = Action { implicit request =>
    Ok(views.html.index())

  }

  def user = Action {implicit request =>
    Ok(views.html.user(userForm))
  }

}

以下user.scala.html文件

@(userForm: Form[UserData])(implicit messages: Messages)

@helper.form(action = routes.HomeController.userPost()) {
  @helper.inputText(userForm("name"), 'id -> "name", 'size -> 30)
  @helper.inputText(userForm("age"))

  
}

以及以下路线文件:

    # Routes
# This file defines all application routes (Higher priority routes first)
# https://www.playframework.com/documentation/latest/ScalaRouting
# ~~~~

# An example controller showing a sample home page
GET     /                           controllers.HomeController.index

# Map static resources from the /public folder to the /assets URL path
GET     /assets/*file               controllers.Assets.versioned(path="/public", file: Asset)

GET /home controllers.HomeController.home

GET /user controllers.HomeController.user

POST /user controllers.HomeController.userPost

有人可以帮助我通过未经授权的页面错误.我不知道它为什么会出现.我只想传递一个简单的表格.

谢谢.



1> 小智..:

根据上面的Alexander B回复,您需要更改user.scala.html中的行

@helper.form(action = routes.HomeController.userPost()) {

@helper.form(action = helper.CSRF(routes.HomeController.userPost())) {

它对我有用.

关于CSRF和Play:https://www.playframework.com/documentation/2.6.x/ScalaCsrf

推荐阅读
N个小灰流_701
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有