您好,登录后才能下订单哦!
在现代的 Web 应用中,验证码(CAPTCHA)是一种常见的安全措施,用于防止自动化脚本或机器人进行恶意操作。Google 提供的 reCAPTCHA 是一种广泛使用的验证码服务,它能够有效地区分人类用户和机器人。本文将介绍如何在 Spring Boot 项目中集成 Google reCAPTCHA 验证码功能。
首先,你需要在 Google reCAPTCHA 官网上注册一个站点,并获取你的站点密钥和密钥。
localhost
用于本地开发)。在 pom.xml
文件中添加以下依赖:
<dependencies>
<!-- Spring Boot Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- Spring Boot Validation -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<!-- Google reCAPTCHA -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
</dependencies>
在 application.properties
文件中配置你的 reCAPTCHA 站点密钥和密钥:
google.recaptcha.key.site=your-site-key
google.recaptcha.key.secret=your-secret-key
创建一个服务类来处理 reCAPTCHA 验证逻辑:
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
@Service
public class ReCaptchaService {
@Value("${google.recaptcha.key.secret}")
private String secretKey;
private static final String GOOGLE_RECAPTCHA_VERIFY_URL = "https://www.google.com/recaptcha/api/siteverify";
public boolean verifyRecaptcha(String response) {
RestTemplate restTemplate = new RestTemplate();
Map<String, String> request = new HashMap<>();
request.put("secret", secretKey);
request.put("response", response);
String result = restTemplate.postForObject(GOOGLE_RECAPTCHA_VERIFY_URL, request, String.class);
Gson gson = new Gson();
ReCaptchaResponse reCaptchaResponse = gson.fromJson(result, ReCaptchaResponse.class);
return reCaptchaResponse.isSuccess();
}
private static class ReCaptchaResponse {
private boolean success;
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
}
}
创建一个控制器来处理表单提交和 reCAPTCHA 验证:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class CaptchaController {
@Autowired
private ReCaptchaService reCaptchaService;
@GetMapping("/")
public String index(Model model) {
model.addAttribute("siteKey", "your-site-key");
return "index";
}
@PostMapping("/submit")
public String submitForm(@RequestParam("g-recaptcha-response") String recaptchaResponse, Model model) {
if (reCaptchaService.verifyRecaptcha(recaptchaResponse)) {
model.addAttribute("message", "验证成功!");
} else {
model.addAttribute("message", "验证失败,请重试。");
}
return "result";
}
}
在 src/main/resources/templates
目录下创建 index.html
和 result.html
文件。
index.html
:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>reCAPTCHA 示例</title>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
</head>
<body>
<h1>reCAPTCHA 示例</h1>
<form action="/submit" method="post">
<div class="g-recaptcha" th:attr="data-sitekey=${siteKey}"></div>
<br/>
<button type="submit">提交</button>
</form>
</body>
</html>
result.html
:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>结果</title>
</head>
<body>
<h1 th:text="${message}"></h1>
<a href="/">返回</a>
</body>
</html>
完成上述步骤后,启动你的 Spring Boot 项目。访问 http://localhost:8080/
,你将看到一个包含 reCAPTCHA 验证码的表单。提交表单后,系统将验证 reCAPTCHA 并返回相应的结果。
通过本文的介绍,你已经学会了如何在 Spring Boot 项目中集成 Google reCAPTCHA 验证码功能。reCAPTCHA 是一种简单而有效的安全措施,能够帮助你的应用防止恶意机器人攻击。希望本文对你有所帮助,祝你在开发过程中一切顺利!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。