景区官方网站建设方案,雅安网站建设,建设商城网站价格,如何建设高等数学课程网站谷歌 recaptcha介绍 Google的reCaptcha是一个库#xff0c;用于防止漫游器将数据提交到您的公共表单或访问您的公共数据。 在本文中#xff0c;我们将研究如何将reCaptcha与基于Spring Boot的Web应用程序集成 设置验证码 您应该从管理面板创建API密钥。 您必须创建一个示例… 谷歌 recaptcha 介绍 Google的reCaptcha是一个库用于防止漫游器将数据提交到您的公共表单或访问您的公共数据。 在本文中我们将研究如何将reCaptcha与基于Spring Boot的Web应用程序集成 设置验证码 您应该从管理面板创建API密钥。 您必须创建一个示例应用程序如下所示 发布您应该能够看到密钥和机密以及一些足以入门的说明如下所示 创建示例Spring Boot应用 像往常一样导航到start.spring.io并按如下所示填写并下载项目 在您喜欢的IDE中打开然后运行RecaptchaDemoApplication并从http// localhost8080访问该应用。 由于未定义控制器因此您将看到错误。 使用表单创建公共页面 我们将利用 基于Bootstrap的主题 jQuery的 jQuery Form插件 jQuery验证插件 敬酒通知 Fontawesome图标 Recaptcha JS 启用了reCaptcha的表单HTML是 form idsignup-form classform-horizontal methodPOST th:action{/api/signup} th:object${user}div classform-grouplabel classcontrol-label requiredFirst Name/labelinput typetext th:field*{firstName} classform-control required //divdiv classform-grouplabel classcontrol-label requiredLast Name/labelinput typetext th:field*{lastName} classform-control required //divdiv classform-grouplabel classcontrol-label requiredEmail/labelinput typetext th:field*{email} classform-control required //divdiv classform-grouplabel classcontrol-label requiredPassword/labelinput typepassword th:field*{password} classform-control required //divdiv classform-grouplabel classcontrol-label requiredConfirm Password/labelinput typepassword th:field*{confirmPassword} classform-control required //divdiv classg-recaptcha data-sitekey6LdGeDcUAAAAALfoMZ2Ltv4EE6AHIYb8nSxhCRh_/divbutton typesubmit classbtn btn-primarySubmit/button/form 上面重要的部分是具有g-recaptcha类的div 它具有公共站点密钥。 另一个密钥应该在您的服务器中安全您可以使用该密钥来验证来自Google服务器的验证码。 另外请确保reCaptcha JS位于“。”之前。 加载URL http// localhost8080 /将呈现以下形式 创建用于表单处理的API 接下来是在处理添加用户API时验证验证码。 Google提供了一个端点我们将在该端点上发布以验证验证码。 以下是验证验证码的代码 Slf4j
Service
public class RecaptchaService {Value(${google.recaptcha.secret}) String recaptchaSecret;private static final String GOOGLE_RECAPTCHA_VERIFY_URL https://www.google.com/recaptcha/api/siteverify;Autowired RestTemplateBuilder restTemplateBuilder;public String verifyRecaptcha(String ip, String recaptchaResponse){MapString, String body new HashMap();body.put(secret, recaptchaSecret);body.put(response, recaptchaResponse);body.put(remoteip, ip);log.debug(Request body for recaptcha: {}, body);ResponseEntityMap recaptchaResponseEntity restTemplateBuilder.build().postForEntity(GOOGLE_RECAPTCHA_VERIFY_URL?secret{secret}response{response}remoteip{remoteip}, body, Map.class, body);log.debug(Response from recaptcha: {}, recaptchaResponseEntity);MapString, Object responseBody recaptchaResponseEntity.getBody();boolean recaptchaSucess (Boolean)responseBody.get(success);if ( !recaptchaSucess) {ListString errorCodes (List)responseBody.get(error-codes);String errorMessage errorCodes.stream().map(s - RecaptchaUtil.RECAPTCHA_ERROR_CODE.get(s)).collect(Collectors.joining(, ));return errorMessage;}else {return StringUtils.EMPTY;}}} 我们创建了一个地图该地图将响应代码与Google提供的响应消息进行映射如下所示 public class RecaptchaUtil {public static final MapString, String RECAPTCHA_ERROR_CODE new HashMap();static {RECAPTCHA_ERROR_CODE.put(missing-input-secret, The secret parameter is missing);RECAPTCHA_ERROR_CODE.put(invalid-input-secret, The secret parameter is invalid or malformed);RECAPTCHA_ERROR_CODE.put(missing-input-response, The response parameter is missing);RECAPTCHA_ERROR_CODE.put(invalid-input-response, The response parameter is invalid or malformed);RECAPTCHA_ERROR_CODE.put(bad-request, The request is invalid or malformed);}
} 让我们以api形式使用RecaptchaService 如下所示 PostMapping(/signup)
public ResponseEntity? signup(Valid User user, RequestParam(nameg-recaptcha-response) String recaptchaResponse,HttpServletRequest request
){String ip request.getRemoteAddr();String captchaVerifyMessage captchaService.verifyRecaptcha(ip, recaptchaResponse);if ( StringUtils.isNotEmpty(captchaVerifyMessage)) {MapString, Object response new HashMap();response.put(message, captchaVerifyMessage);return ResponseEntity.badRequest().body(response);}userRepository.save(user);return ResponseEntity.ok().build();
} UI上的验证码通过键g-recaptcha-response作为响应参数传递到g-recaptcha-response 。 因此我们使用此响应密钥和选项ip地址调用验证码验证服务。 验证的结果是成功还是失败。 如果消息失败我们将捕获该消息并将其返回给客户端。 此示例的完整代码可以在这里找到。 翻译自: https://www.javacodegeeks.com/2017/11/using-google-recaptcha-spring-boot-application.html谷歌 recaptcha