溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

springbooot使用google驗證碼的功能怎么實現

發布時間:2023-05-04 16:50:02 來源:億速云 閱讀:396 作者:iii 欄目:開發技術

Spring Boot 使用 Google 驗證碼的功能怎么實現

在現代的 Web 應用中,驗證碼(CAPTCHA)是一種常見的安全措施,用于防止自動化腳本或機器人進行惡意操作。Google 提供的 reCAPTCHA 是一種廣泛使用的驗證碼服務,它能夠有效地區分人類用戶和機器人。本文將介紹如何在 Spring Boot 項目中集成 Google reCAPTCHA 驗證碼功能。

1. 注冊 Google reCAPTCHA

首先,你需要在 Google reCAPTCHA 官網上注冊一個站點,并獲取你的站點密鑰和密鑰。

  1. 訪問 Google reCAPTCHA 頁面。
  2. 填寫站點名稱、選擇 reCAPTCHA 類型(通常選擇 reCAPTCHA v2)。
  3. 輸入你的域名(例如 localhost 用于本地開發)。
  4. 點擊“提交”按鈕,獲取你的站點密鑰(Site Key)和密鑰(Secret Key)。

2. 配置 Spring Boot 項目

2.1 添加依賴

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>

2.2 配置 reCAPTCHA 密鑰

application.properties 文件中配置你的 reCAPTCHA 站點密鑰和密鑰:

google.recaptcha.key.site=your-site-key
google.recaptcha.key.secret=your-secret-key

2.3 創建 reCAPTCHA 服務類

創建一個服務類來處理 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;
        }
    }
}

2.4 創建控制器

創建一個控制器來處理表單提交和 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";
    }
}

2.5 創建 Thymeleaf 模板

src/main/resources/templates 目錄下創建 index.htmlresult.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>

3. 運行項目

完成上述步驟后,啟動你的 Spring Boot 項目。訪問 http://localhost:8080/,你將看到一個包含 reCAPTCHA 驗證碼的表單。提交表單后,系統將驗證 reCAPTCHA 并返回相應的結果。

4. 總結

通過本文的介紹,你已經學會了如何在 Spring Boot 項目中集成 Google reCAPTCHA 驗證碼功能。reCAPTCHA 是一種簡單而有效的安全措施,能夠幫助你的應用防止惡意機器人攻擊。希望本文對你有所幫助,祝你在開發過程中一切順利!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女