gpt4 book ai didi

java - 使用 scala 和 play 框架进行 Recaptcha

转载 作者:行者123 更新时间:2023-12-02 05:35:26 25 4
gpt4 key购买 nike

通过使用tutorial ,我正在尝试在我的游戏项目中应用验证码。

HTML

<form action="/applyForWork" method="post" enctype="multipart/form-data">
<input type="text" name="relevant" id="relevant" >
<input type="file" name="file"/>
<br/>
@Html(views.ReCaptcha.render())
<br/>
<input type="submit" value="Upload"/>
</form>

Controller

def applyForWork = Action {
implicit request =>
println(request.body.asFormUrlEncoded) //None
Ok("submitted")
}

Q1.why this println(request.body.asFormUrlEncoded) gives None?

<小时/>

Q2.captcha box is shown in my html but how to validate it is correct or not?

我正在使用 scala 2.10 和 play Framework 2.2

最佳答案

A1。原因是您表单的enctype。当您使用multipart/form-data时,您可以通过以下方式访问表单数据:

request.body.asMultipartFormData

A2。不管怎样,如果我是你,我会坚持你提到的教程中提出的解决方案,并为 recaptcha 值创建表单映射。

case class CaptchaForm(challenge: String, response: String)

val captchaForm = Form[CaptchaForm](
mapping(
"recaptcha_challenge_field" -> nonEmptyText,
"recaptcha_response_field" -> nonEmptyText
)(CaptchaForm.apply)(CaptchaForm.unapply)
)

这样您就可以在任何需要处理 Repatcha 的地方重复使用它。

def applyForWork = Action { implicit request =>
captchaForm.bindFromRequest.fold(
formWithErrors => BadRequest("Captcha Param Error"),
captchaForm => {
println(captchaForm.challenge)
println(captchaForm.response)
if (check(request.remoteAddress, captchaForm.challenge, captchaForm.response)) {
//Captcha ok
}
}
)
}

def check(remoteAddress: String, challenge: String, response: String): Boolean = {
val reCaptcha = new ReCaptchaImpl()
reCaptcha.setPrivateKey(privateKey())
val reCaptchaResponse = reCaptcha.checkAnswer(remoteAddress, challenge, response)
reCaptchaResponse.isValid()
}

提示

考虑在模板中使用路由映射而不是硬编码 URL。在这种情况下替换

action="/applyForWork"

action="@routes.YourFormController.handleAction()"

如果您将映射更改为路由中的操作,则无需更改所有使用它的模板。

关于java - 使用 scala 和 play 框架进行 Recaptcha,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25009557/

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