Spring mvc 谷歌验证码总是失败,没有错误代码
在网页中添加了reCAPTCHA v2 API,但我无法使其正常工作。我已经在html页面中配置了添加验证码的密钥Spring mvc 谷歌验证码总是失败,没有错误代码,spring-mvc,spring-boot,recaptcha,Spring Mvc,Spring Boot,Recaptcha,在网页中添加了reCAPTCHA v2 API,但我无法使其正常工作。我已经在html页面中配置了添加验证码的密钥 <!-- Google reCAPTCHA --> <script th:src="@{https://www.google.com/recaptcha/api.js}"></script> 在服务器端(使用Spring) @RequestMapping(value=“/contact”,method=RequestMethod
<!-- Google reCAPTCHA -->
<script th:src="@{https://www.google.com/recaptcha/api.js}"></script>
在服务器端(使用Spring)
@RequestMapping(value=“/contact”,method=RequestMethod.POST)
公共可调用的contactQuery(final@modeldattribute(“contact”)@有效的ContactForm ContactForm,
final@RequestParam(“g-recaptcha-response”)字符串captchaResponse,final BindingResult,
最终模型(模型){
.....
}
然后在另一节课上
@Service
public class ReCaptchaResponseVerfier {
@Resource
private Environment environment;
private static final Logger LOGGER = LoggerFactory.getLogger(ReCaptchaResponseVerfier.class);
@Async
public Future<GoogleCaptchaResponseData> isCaptchaResponseValid(String captchaResponse) throws InterruptedException,
ExecutionException {
LOGGER.debug(" Validating captcha {}", captchaResponse);
if (captchaResponse.isEmpty()) {
GoogleCaptchaResponseData response = new GoogleCaptchaResponseData();
response.setSuccess(false);
response.setErrorCodes("The \"I am not a robot\" genie says you didn't verify, please do so.");
return new AsyncResult<GoogleCaptchaResponseData>(response);
}
AsyncRestTemplate restTemplate = new AsyncRestTemplate();
Map<String, String> uriVariables = new HashMap<String, String>();
ListenableFuture<ResponseEntity<GoogleCaptchaResponseData>> futureResponse = restTemplate.postForEntity(
"https://www.google.com/recaptcha/api/siteverify", buildCaptachRequest(captchaResponse),
GoogleCaptchaResponseData.class);
GoogleCaptchaResponseData response;
response = futureResponse.get().getBody();
return new AsyncResult<GoogleCaptchaResponseData>(response);
}
private HttpEntity<GoogleCaptchaRequestData> buildCaptachRequest(String captchaResponse) {
GoogleCaptchaRequestData request = new GoogleCaptchaRequestData();
request.setResponse(captchaResponse);
request.setSecret(environment.getProperty("google.recaptcha.secret"));
return new HttpEntity<GoogleCaptchaRequestData>(request);
}
@服务
公共类RecaptCharResponseEverfier{
@资源
私人环境;
私有静态最终记录器Logger=LoggerFactory.getLogger(ReCaptchaResponseVerfier.class);
@异步的
public Future IsCaptCharResponseValid(字符串CaptCharResponse)抛出InterruptedException,
执行例外{
debug(“验证captcha{}”,captchaResponse);
if(captchaResponse.isEmpty()){
GoogleCaptCharResponseData响应=新建GoogleCaptCharResponseData();
response.setSuccess(false);
setErrorCodes(“我不是机器人”精灵说你没有验证,请验证。”);
返回新的异步结果(响应);
}
AsyncRestTemplate restTemplate=新的AsyncRestTemplate();
Map uriVariables=newhashmap();
ListenableFutureResponse=restTemplate.postForEntity(
"https://www.google.com/recaptcha/api/siteverify,buildCaptachRequest(CaptCharResponse),
GoogleCaptchaResponseData.class);
GoogleCaptCharResponseData响应;
response=futuresponse.get().getBody();
返回新的异步结果(响应);
}
私有HttpEntity buildCaptachRequest(字符串CaptCharResponse){
GoogleCaptchaRequestData请求=新建GoogleCaptchaRequestData();
request.setResponse(captchaResponse);
request.setSecret(environment.getProperty(“google.recaptcha.secret”);
返回新的HttpEntity(请求);
}
然而,我总是获得成功:错误响应,即使验证码与页面上的匹配。问题在于请求,在它提到的发送POST请求的文档上,而在注册密钥的页面上是get request。更改为get request,它现在可以工作了。请提供GoogleCaptCharResponseData sourceIts sorted.p问题出现在文档中。在一个地方发布,在另一个地方获取。获取是正确的方法。我正在尝试实现recaptcha服务器验证。如果你能提供你的代码,这将有助于在这里发布mt
@RequestMapping(value = "/contact", method = RequestMethod.POST)
public Callable<String> contactQuery(final @ModelAttribute("contact") @Valid ContactForm contactForm,
final @RequestParam("g-recaptcha-response") String captchaResponse, final BindingResult bindingResult,
final Model model) {
.....
}
@Service
public class ReCaptchaResponseVerfier {
@Resource
private Environment environment;
private static final Logger LOGGER = LoggerFactory.getLogger(ReCaptchaResponseVerfier.class);
@Async
public Future<GoogleCaptchaResponseData> isCaptchaResponseValid(String captchaResponse) throws InterruptedException,
ExecutionException {
LOGGER.debug(" Validating captcha {}", captchaResponse);
if (captchaResponse.isEmpty()) {
GoogleCaptchaResponseData response = new GoogleCaptchaResponseData();
response.setSuccess(false);
response.setErrorCodes("The \"I am not a robot\" genie says you didn't verify, please do so.");
return new AsyncResult<GoogleCaptchaResponseData>(response);
}
AsyncRestTemplate restTemplate = new AsyncRestTemplate();
Map<String, String> uriVariables = new HashMap<String, String>();
ListenableFuture<ResponseEntity<GoogleCaptchaResponseData>> futureResponse = restTemplate.postForEntity(
"https://www.google.com/recaptcha/api/siteverify", buildCaptachRequest(captchaResponse),
GoogleCaptchaResponseData.class);
GoogleCaptchaResponseData response;
response = futureResponse.get().getBody();
return new AsyncResult<GoogleCaptchaResponseData>(response);
}
private HttpEntity<GoogleCaptchaRequestData> buildCaptachRequest(String captchaResponse) {
GoogleCaptchaRequestData request = new GoogleCaptchaRequestData();
request.setResponse(captchaResponse);
request.setSecret(environment.getProperty("google.recaptcha.secret"));
return new HttpEntity<GoogleCaptchaRequestData>(request);
}