pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.captcha</groupId>
<artifactId>captcha</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>captcha</name>
<description>Captcha project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- web层 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 页面渲染jar包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--kaptcha验证码生成器 -->
<dependency>
<groupId>com.github.axet</groupId>
<artifactId>kaptcha</artifactId>
<version>0.0.9</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置的文件application.yml
spring:
thymeleaf:
prefix: classpath:/templates/
check-template-location: true
suffix: .html
encoding: UTF-8
mode: LEGACYHTML5
cache: false
h5页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<div class='loginDiv'>
<h1 align="center">欢迎登录</h1>
<form align="center" action="userLogin" method="get">
<div style="margin-bottom:10px;">
<label>验证码</label> <input style="width:150px; height=100px;" type="text" name="kaptcha"
placeholder="请输入验证码" autocomplete="off">
</div>
<div style="margin-bottom:10px;">
<img src="captcha.jpg" onclick="refresh(this)">
</div>
<div >
<button>立即提交</button>
</div>
</form>
</div>
<script>
function refresh(obj) {
obj.src = "captcha.jpg?t=" + new Date().getMilliseconds();
}
</script>
</body>
</html>
需注入的依赖类
package com.captcha.demo;
import java.util.Properties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
@Configuration
public class KaptchaConfig {
@Bean
public DefaultKaptcha defaultKaptcha(){
Properties properties = new Properties();
properties.setProperty("kaptcha.border","no");
properties.setProperty("kaptcha.textproducer.font.color","black");
properties.setProperty("kaptcha.textproducer.char.space","5");
Config config = new Config(properties);
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}
最后是web层的Controller Demo
package com.captcha.demo;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.google.code.kaptcha.impl.DefaultKaptcha;
@Controller
public class CaptchDemo {
@Autowired
private DefaultKaptcha defaultKaptcha;
@RequestMapping("/captcha.jpg")
@ResponseBody
public R applyCheckCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setHeader("Cache-Control", "no-store, no-cache");
response.setContentType("image/jpeg");
// 生成文字验证码
String text = defaultKaptcha.createText();
// 生成图片验证码
BufferedImage image = defaultKaptcha.createImage(text);
// 保存到session
request.getSession().setAttribute("CHECK CODE", text);
ServletOutputStream out = response.getOutputStream();
ImageIO.write(image, "jpg", out);
return null;
}
@ResponseBody
@RequestMapping(value = "/userLogin")
public String userLogin(HttpServletRequest request) {
String kaptcha = request.getParameter("kaptcha");
String s = request.getSession().getAttribute("CHECK CODE").toString();
if (StringUtils.isEmpty(kaptcha) || !s.equals(kaptcha)) {
return "failure";
}
return "success";
}
@RequestMapping(value = "/login")
public String login() {
return "/login";
}
}
以上就是使用springboot创建的验证码生成与验证全过程,如果有什么问题可在下面评论提出,谢谢!