创建SpringBoot项目很容易。
.
2 .引入2.POM.XMLkaptchamaven依赖关系
从属关系
groupid com.github.pengg le/groupid
artifactIdkaptcha/artifactId
版本2.3.2 /版本
/从属关系
将资源/应用程序. yml设置为添加到:
kaptcha:
是否有边框,默认值为“是”、“是”和“否”
border.color:10、19、90//边框颜色
文本处理器:
字体:
颜色:蓝色
size: 30
names:宋体、楷体、微软雅黑//文本字体样式
char:
长度: 6
图像:
width: 120
height: 40
会话:
key:代码
4 .将K aptchaConfig.java添加到4.src/main/java/config配置类中
导入Java.util .属性;
importorg.spring框架. beans.factory .分析.值;
importorg.spring框架.上下文.注释. bean;
importorg.spring框架. stereotype.com组件;
import com.Google.code.kap tcha.impl.Defaultkaptcha
import com .谷歌.代码. kap tcha.Util.config;
//*
* @Description Kaptcha配置类用于生成图形授权码
*/
@Component
公共类kaptchachaconfig
@value(($ ) kaptcha.border ) ) )
私有字符串边框;
@value(($ ) kaptcha.border.color ) )
私有字符串边框颜色;
@值(($ ) kap tcha.text producer.font.color ) )
私有文本产品颜色;
@值(($ ) kaptcha.textproducer.font.size ) )
私有文本产品字体大小;
@值(($ ) kap tcha.text producer.font.names ) )
私有文本产品名称;
@值(($ ) kap tcha.text producer.char.length ) )
privatestringtextproducercharlength
@value(($ ) kaptcha.image.width ) )
私有字符串图像宽度;
@value(($ ) kaptcha.image.height ) )
私有字符串图像高度;
@value(($ ) kaptcha.session.key ) ) )
专用字符串会话密钥;
@Bean
publicdefaultkaptchagetdefaultkapcha (
defaultkaptchadefaultkaptcha=newdefaultkaptcha (;
属性属性属性=新属性(;
System.out.println(border);properties.setProperty("kaptcha.border", "yes");
properties.setProperty("kaptcha.border.color", borderColor);
properties.setProperty("kaptcha.textproducer.font.color", textproducerFontColor);
properties.setProperty("kaptcha.textproducer.font.size", textproducerFontSize);
properties.setProperty("kaptcha.textproducer.font.names", textproducerFontNames);
properties.setProperty("kaptcha.textproducer.char.length", textproducerCharLength);
properties.setProperty("kaptcha.image.width", imageWidth);
properties.setProperty("kaptcha.image.height", imageHeight);
properties.setProperty("kaptcha.session.key", sessionKey);
Config config = new Config(properties);
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}
5.src/main/java/Controller加入KaptchaController.java
a.通过配置类DefaultKaptcha.java从application.yml中获取到配置属性,通过@Bean注入配
b.使用@Autowired注入Kaptcha,在他类中进行注入此类
private DefaultKaptcha defaultKaptcha;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.jeecg.modules.online.config.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import lombok.extern.slf4j.Slf4j;
@CrossOrigin
@Slf4j
@RestController
@RequestMapping(value = "/Kaptcha")
public class KaptchaController {
/* 注入Kaptcha */
@Autowired
private DefaultKaptcha defaultKaptcha;
@GetMapping(value = "/code")
//public Result defaultKaptcha(HttpServletRequest request, HttpServletResponse response){
public void defaultKaptcha(HttpServletRequest request, HttpServletResponse response){
byte[] captchaChallengeAsJpeg;
ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
/**
* 生成验证码字符串并保存到session中
*/
String createText = defaultKaptcha.createText();
HttpSession session = request.getSession();
//session.setAttribute(SessionConstant.IMAGE_CODE, createText);
/**
* 使用生成的验证码字符串返回一个BufferedImage对象并转为byte写入到byte数组中
*/
BufferedImage challenge = defaultKaptcha.createImage(createText);
try {
ImageIO.write(challenge,"jpg",jpegOutputStream);
} catch (IOException e) {
log.error("生成图形验证码失败",e);
//throw new BusinessException(ErrorEnum.CRATE_IMAGE_ERROR);// 抛出异常,可以不用关心
}
/**
* 定义response输出类型为image/jpeg类型,使用response输出流输出图片的byte数组
*/
captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
response.setHeader("Cache-Control", "no-store");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires",0);
response.setContentType("image/jpeg");
try {
ServletOutputStream servletOutputStream = response.getOutputStream();
servletOutputStream.write(captchaChallengeAsJpeg);
servletOutputStream.flush();
servletOutputStream.close();
} catch (IOException e) {
log.error("输出验证码失败",e);
//throw new BusinessException(ErrorEnum.CRATE_IMAGE_ERROR);// 抛出异常,可以不用关心
}
//return ResultUtil.success("sucess");// 返回成功提示信息
ResultUtil.success("sucess");// 返回成功提示信息
}
}
6.html页面调用此控制类生成验证码
<div>
<img id="verification" src="/verification" style="cursor: pointer;" title="看不清?换一张" />
</div>
<script>
$(function () {
// 刷新验证码
$("#verification").bind("click", function () {
//$(this).hide().attr('src', '/verification?random=' + Math.random()).fadeIn();
$(this).show().attr('src', '/Kaptcha/code').fadeIn();
});
});
7.页面显示刷新验证码,成功