谷歌 recaptcha

介绍

Google的reCaptcha是一个库,用于防止漫游器将数据提交到您的公共表单或访问您的公共数据。

在本文中,我们将研究如何将reCaptcha与基于Spring Boot的Web应用程序集成

设置验证码

您应该从管理面板创建API密钥。 您必须创建一个示例应用程序,如下所示:

发布您应该能够看到密钥和机密以及一些足以入门的说明,如下所示:

创建示例Spring Boot应用

像往常一样,导航到start.spring.io并按如下所示填写并下载项目:

在您喜欢的IDE中打开,然后运行RecaptchaDemoApplication并从http:// localhost:8080访问该应用。 由于未定义控制器,因此您将看到错误。

使用表单创建公共页面

我们将利用:

  • 基于Bootstrap的主题
  • jQuery的
  • jQuery Form插件
  • jQuery验证插件
  • 敬酒通知
  • Fontawesome图标
  • Recaptcha JS

启用了reCaptcha的表单HTML是:

<form id="signup-form" class="form-horizontal" method="POST" th:action="@{/api/signup}" th:object="${user}"><div class="form-group"><label class="control-label required">First Name</label><input type="text" th:field="*{firstName}" class="form-control required" /></div><div class="form-group"><label class="control-label required">Last Name</label><input type="text" th:field="*{lastName}" class="form-control required" /></div><div class="form-group"><label class="control-label required">Email</label><input type="text" th:field="*{email}" class="form-control required" /></div><div class="form-group"><label class="control-label required">Password</label><input type="password" th:field="*{password}" class="form-control required" /></div><div class="form-group"><label class="control-label required">Confirm Password</label><input type="password" th:field="*{confirmPassword}" class="form-control required" /></div><div class="g-recaptcha" data-sitekey="6LdGeDcUAAAAALfoMZ2Ltv4EE6AHIYb8nSxhCRh_"></div><button type="submit" class="btn btn-primary">Submit</button></form>

上面重要的部分是具有g-recaptcha类的div ,它具有公共站点密钥。 另一个密钥应该在您的服务器中安全,您可以使用该密钥来验证来自Google服务器的验证码。 另外,请确保reCaptcha JS位于“。”之前。

加载URL http:// localhost:8080 /将呈现以下形式:

创建用于表单处理的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){Map<String, String> body = new HashMap<>();body.put("secret", recaptchaSecret);body.put("response", recaptchaResponse);body.put("remoteip", ip);log.debug("Request body for recaptcha: {}", body);ResponseEntity<Map> recaptchaResponseEntity = restTemplateBuilder.build().postForEntity(GOOGLE_RECAPTCHA_VERIFY_URL+"?secret={secret}&response={response}&remoteip={remoteip}", body, Map.class, body);log.debug("Response from recaptcha: {}", recaptchaResponseEntity);Map<String, Object> responseBody = recaptchaResponseEntity.getBody();boolean recaptchaSucess = (Boolean)responseBody.get("success");if ( !recaptchaSucess) {List<String> 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 Map<String, 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(name="g-recaptcha-response") String recaptchaResponse,HttpServletRequest request
){String ip = request.getRemoteAddr();String captchaVerifyMessage = captchaService.verifyRecaptcha(ip, recaptchaResponse);if ( StringUtils.isNotEmpty(captchaVerifyMessage)) {Map<String, 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

谷歌 recaptcha_在Spring Boot应用程序中使用Google reCaptcha相关推荐

  1. 如何在Spring Boot应用程序中使用配置文件

    你好朋友, 在本教程中,我们将学习如何在Spring Boot应用程序中使用配置文件. 我们将在本教程中讨论以下几点: 1.什么是Spring Boot Profile,为什么我们需要分析 2.如何使 ...

  2. 在使用Gradle构建的Spring Boot应用程序中覆盖Spring Framework版本

    如果要使用或仅通过Spring Boot检查Spring的最新版本,但当前的Spring Boot版本取决于旧的Spring版本,则需要稍微调整Gradle构建配置. 例如,在撰写本文时,Spring ...

  3. 在Spring Boot应用程序中测试邮件代码

    在构建Spring Boot应用程序时,您可能会需要添加邮件配置. 实际上,在Spring Boot中配置邮件与在Spring Bootless应用程序中配置邮件没有太大区别. 但是,如何测试邮件配置 ...

  4. cloud foundry_将Spring Boot应用程序绑定到Cloud Foundry中的服务的方法

    cloud foundry 如果要试用Cloud Foundry ,最简单的方法是下载出色的PCF开发人员或在Pivotal Web Services站点上创建试用帐户. 其余文章假定您已经安装了Cl ...

  5. 将Spring Boot应用程序部署到Tomcat中

    "我喜欢编写身份验证和授权代码." 〜从来没有Java开发人员. 厌倦了一次又一次地建立相同的登录屏幕? 尝试使用Okta API进行托管身份验证,授权和多因素身份验证. 部署应用 ...

  6. 将Spring Boot应用程序绑定到Cloud Foundry中的服务的方法

    如果您想试用Cloud Foundry ,最简单的方法是下载出色的PCF开发人员或在Pivotal Web Services站点上创建试用帐户. 文章的其余部分假定您已经安装了Cloud Foundr ...

  7. Spring Boot微服务中Chaos Monkey的应用

    点击蓝色"程序猿DD"关注我哟 有多少人从未在生产环境中遇到系统崩溃或故障?当然,你们每个人迟早都会经历它.如果我们无法避免失败,那么解决方案似乎是将我们的系统维持在永久性故障状态 ...

  8. cognito_将Spring Boot应用程序与Amazon Cognito集成

    cognito 在本文中,我们将展示如何使用Spring Security 5.0中引入的OAuth 2.0客户端库 ,在Spring Boot应用程序中为身份验证用户使用Amazon Cognito ...

  9. jrebel gradle_JRebel适用于Gradle Spring Boot应用程序

    jrebel gradle 关于如何将JRebel添加到使用Gradle作为构建工具的Spring Boot应用程序中,有一些文档 . 它是基本的,但是效果很好. 您所要做的就是在build.grad ...

最新文章

  1. 只用嘴唇动一动,AI就能合成语音,效果自然流畅看不出破绽
  2. c#判断右键菜单(ContextMenuStrip)是从哪个控件弹出来的方法
  3. 超图桌面版使用模板创建数据源
  4. 【Python】分享几个用Python给图片添加水印的方法,简单实用
  5. 第3章 Python 数字图像处理(DIP) - 灰度变换与空间滤波13 - 平滑低通滤波器 -盒式滤波器核
  6. python基础(十三)
  7. cesium 加载网格
  8. js实现web贪吃蛇小游戏
  9. python utf编码 查询_python数据库查询中文乱码
  10. 网络流行简笔画图片大全,互联网图标简笔画
  11. 建筑设计的未来是什么?| 建筑 · 人工智能专栏
  12. 鲁迅朱安:留给世纪的背影_拔剑-浆糊的传说_新浪博客
  13. slideUp()方法和slideDown()方法
  14. 收藏 | 堪称神器的42款Chrome插件
  15. solidworks3D打印技术
  16. char数组存储中英文字符
  17. 纯JavaScript山寨腾讯手机游戏《天天爱消除》开发过程详细
  18. HTML期末大作业 : 个人网页制作 大学生个人网页设计 个人网站模板 简单静态HTML个人网页作品...
  19. 国内云服务器厂商众多,如何选择
  20. Swin UNETR: Swin Transformers for Semantic Segmentation of Brain Tumors in MRI Images

热门文章

  1. 带旋treap概念及模板,带例题:普通平衡树
  2. 二分算法:平均值(洛谷 UVA1451)
  3. 欢乐纪中A组赛【2019.8.9】
  4. 欢乐SSL初二组周六赛【2019.4.27】
  5. jzoj3511-cza的蛋糕【状态压缩dp,dfs】
  6. ssl2340-格子游戏【并查集】
  7. ssl1312ZP2502-[HAOI2006]旅行【图论,并查集】
  8. 【Trie】阅读理解(luogu 3879/ybtoj Trie-4)
  9. JavaFX官方教程(八)之JavaFX中的动画和视觉效果
  10. ArrayList基操