Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用scala和play框架重温TCHA_Java_Scala_Playframework_Captcha_Recaptcha - Fatal编程技术网

Java 使用scala和play框架重温TCHA

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。无论如何,如果我是你,我会坚持你提到的

通过使用,我试图在我的游戏项目中应用验证码

HTML

Q1.为什么这个
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()"