在現代的 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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。