您好,登录后才能下订单哦!
在现代Web应用中,验证码(CAPTCHA)是一种常见的安全机制,用于防止自动化脚本或机器人恶意攻击。Spring Boot流行的Java框架,提供了便捷的方式来生成和验证验证码。本文将详细介绍如何在Spring Boot项目中实现验证码的生成及验证功能。
首先,我们需要在pom.xml
中添加生成验证码所需的依赖。常用的验证码生成库有kaptcha
和captcha
等。这里我们以kaptcha
为例:
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
接下来,我们需要在Spring Boot中配置Kaptcha。可以通过Java配置类来实现:
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;
@Configuration
public class KaptchaConfig {
@Bean
public DefaultKaptcha getDefaultKaptcha() {
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
Properties properties = new Properties();
properties.setProperty("kaptcha.border", "yes");
properties.setProperty("kaptcha.border.color", "105,179,90");
properties.setProperty("kaptcha.textproducer.font.color", "blue");
properties.setProperty("kaptcha.image.width", "110");
properties.setProperty("kaptcha.image.height", "40");
properties.setProperty("kaptcha.textproducer.font.size", "30");
properties.setProperty("kaptcha.session.key", "code");
properties.setProperty("kaptcha.textproducer.char.length", "4");
properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
Config config = new Config(properties);
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}
在控制器中,我们可以通过注入DefaultKaptcha
来生成验证码,并将其存储到Session中:
import com.google.code.kaptcha.impl.DefaultKaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
@RestController
@RequestMapping("/captcha")
public class CaptchaController {
@Autowired
private DefaultKaptcha defaultKaptcha;
@GetMapping("/generate")
public void generateCaptcha(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
response.setHeader("Pragma", "no-cache");
response.setContentType("image/jpeg");
String capText = defaultKaptcha.createText();
HttpSession session = request.getSession();
session.setAttribute("captcha", capText);
BufferedImage bi = defaultKaptcha.createImage(capText);
ServletOutputStream out = response.getOutputStream();
ImageIO.write(bi, "jpg", out);
out.flush();
out.close();
}
}
在前端页面中,我们可以通过<img>
标签来显示生成的验证码图片,并通过表单提交用户输入的验证码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>验证码验证</title>
</head>
<body>
<form action="/captcha/validate" method="post">
<img src="/captcha/generate" alt="验证码" onclick="this.src='/captcha/generate?'+Math.random();">
<input type="text" name="captcha" placeholder="请输入验证码">
<button type="submit">提交</button>
</form>
</body>
</html>
在后端控制器中,我们可以通过比较用户输入的验证码和Session中存储的验证码来进行验证:
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@RestController
@RequestMapping("/captcha")
public class CaptchaController {
@PostMapping("/validate")
public String validateCaptcha(@RequestParam("captcha") String captcha, HttpServletRequest request) {
HttpSession session = request.getSession();
String sessionCaptcha = (String) session.getAttribute("captcha");
if (captcha.equalsIgnoreCase(sessionCaptcha)) {
return "验证码正确";
} else {
return "验证码错误";
}
}
}
通过以上步骤,我们可以在Spring Boot项目中轻松实现验证码的生成和验证功能。验证码的生成依赖于kaptcha
库,而验证则通过比较用户输入和Session中存储的验证码来完成。这种机制可以有效防止自动化脚本的攻击,提升Web应用的安全性。
在实际应用中,还可以根据需求对验证码的样式、长度、复杂度等进行调整,以进一步提高安全性。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。