日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

谷歌 recaptcha_在Spring Boot应用程序中使用Google reCaptcha

發布時間:2023/12/3 javascript 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 谷歌 recaptcha_在Spring Boot应用程序中使用Google reCaptcha 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

谷歌 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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。