Java 使用scala和play框架重温TCHA
通过使用,我试图在我的游戏项目中应用验证码 HTML Q1.为什么这个Java 使用scala和play框架重温TCHA,java,scala,playframework,captcha,recaptcha,Java,Scala,Playframework,Captcha,Recaptcha,通过使用,我试图在我的游戏项目中应用验证码 HTML Q1.为什么这个println(request.body.asFormUrlEncoded)给出无 Q2.captcha框显示在我的html中,但如何验证它是否正确 还是不 我将scala 2.10与play framework 2.2一起使用。原因是您的表单类型不同。当您使用多部分/表单数据时,您可以通过以下方式访问表单数据: request.body.asMultipartFormData A2。无论如何,如果我是你,我会坚持你提到的
println(request.body.asFormUrlEncoded)
给出无
Q2.captcha框显示在我的html中,但如何验证它是否正确 还是不 我将scala 2.10与play framework 2.2一起使用。原因是您的表单类型不同。当您使用多部分/表单数据时,您可以通过以下方式访问表单数据:
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"
与
如果将映射更改为路由中的某个操作,则不必更改所有使用它的模板。什么是
映射
?我发现错误未找到:值映射
尝试导入play.api.data.Forms_
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()
}
action="/applyForWork"
action="@routes.YourFormController.handleAction()"