前排提示:该生成验证码的步骤在ssm与springboot都可以使用,这里推荐使用springboot如果要使用ssm要进行适当的修改。
1、导入验证码依赖
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
2、创建KaptchaConfig.class(推荐创建在config包下)
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// kaptcha 的配置文件
public class KaptchaConfig {
@Bean//注入IOC容器
public DefaultKaptcha getDefaultKaptcha(){
DefaultKaptcha captchaProducer = 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);
captchaProducer.setConfig(config);
return captchaProducer;
}
}
3、创建CodeUtil.class(推荐创建在utils包下)
import javax.servlet.http.HttpServletRequest;
public class CodeUtil {
//将获取到的前端参数转为string类型
public static String getString(HttpServletRequest request, String key) {
try {
String result = request.getParameter(key);
if(result != null) {
result = result.trim();
}
if("".equals(result)) {
result = null;
}
return result;
}catch(Exception e) {
return null;
}
}
//验证码校验
public static boolean checkVerifyCode(HttpServletRequest request) {
//获取生成的验证码
String verifyCodeExpected = (String) request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
System.out.println("生成的验证码为:"+verifyCodeExpected);
//获取用户输入的验证码
String verifyCodeActual = CodeUtil.getString(request, "verifyCodeActual");
System.out.println("用户输入的验证码:"+verifyCodeActual);
if(verifyCodeActual == null ||!verifyCodeActual.equals(verifyCodeExpected)) {
return false;
}
return true;
}
}
4、创建CodeController.class(推荐创建在controller包下)
import com.example.project_test.Utils.CodeUtil;
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
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;
@Controller //验证码控制层
public class CodeController {
@Autowired
private Producer captchaProducer=null;
@RequestMapping("/kaptcha")
public void getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws IOException {
HttpSession session = request.getSession();
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=captchaProducer.createText();
session.setAttribute(Constants.KAPTCHA_SESSION_KEY,capText);
//向客户端写出
BufferedImage bi = captchaProducer.createImage(capText);
ServletOutputStream out = response.getOutputStream();
ImageIO.write(bi, "jpg", out);
try {
out.flush();
} finally {
out.close();
}
}
//跳转页面 跳转到测试页面 也就是有验证码的页面
@RequestMapping("/jst/t1")
public String toT1(){
return "/justify/t1";
}
//调用工具类判断验证码的正确性
@RequestMapping("/check")
public String test(HttpServletRequest request){
CodeUtil.checkVerifyCode(request);
return "/justify/t1";
}
}
5、创建显示页面justify.t1.html(在template包下)
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<form th:action="@{/check}">
<label>验证码:</label>
<input type="text" name="verifyCodeActual"><br><br>
<img th:src="@{/kaptcha}" onclick="refresh()" alt="无法显示图片" title="点击换图" id="captcha_img"><br><br>
<input type="submit" value="提交">
</form>
<script>
<!-- 验证码刷新-->
function refresh() {
$("#captcha_img").attr("src", "");
$("#captcha_img").attr("src", "/kaptcha");
}
</script>
</body>
</html>
6、图片展示
7、输入点击提交后控制台显示输出(在CodeUtil)
有不懂的留评论。