javascript
谷歌 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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 英雄联盟s8灵魂符文(英雄联盟灵魂行者符
- 下一篇: 电脑100兆网线接法(电脑100兆网线接