springbooot使用google验证码的功能怎么实现

发布时间:2023-05-04 16:50:02 作者:iii
来源:亿速云 阅读:362

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 是一种简单而有效的安全措施,能够帮助你的应用防止恶意机器人攻击。希望本文对你有所帮助,祝你在开发过程中一切顺利!

推荐阅读:
  1. RK3399嵌入式开发板Google Fastboot重启设置的方法是什么
  2. Google照片现在可以搜索照片中的文字

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

google

上一篇:springboot对压缩请求的处理方法是什么

下一篇:Sync.Pool怎么提升Go程序性能

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》