gpt4 book ai didi

scala - 未经授权的: You must be authenticated to access this page.-通过 Play 表格时

转载 作者:行者123 更新时间:2023-12-04 04:09:56 25 4
gpt4 key购买 nike

现在,我知道我可能没有清晰的代码,但我只希望该死的东西能够正常工作。它给我一个“未经授权。您必须经过身份验证才能访问此页面”错误。

我有以下 Controller

    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"))

<input type="submit" value="submit"</input>
}

和以下路由文件:
    # 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

有人可以帮助我克服“未经授权”页面错误。我不知道为什么会这样。我只想通过一个简单的表格。

谢谢你。

最佳答案

根据上面的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

关于scala - 未经授权的: You must be authenticated to access this page.-通过 Play 表格时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41684661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com