1.添加依赖包
<!-- 验证码生成器-->
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
2.在application.yml文件配置
kaptcha: #验证码配置
fontSize: 40 #字体大小
noiseColor: BLACK #干扰线颜色
imageWidth: 200 #图片宽度
imageHeight: 40 #图片高度
3.编写 配置类
a. KaptchaProps(获取application.yml文件的kaptcha配置)
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "kaptcha")
@Data
public class KaptchaProps {
private String fontSize;
private String noiseColor;
private String imageWidth;
private String imageHeight;
}
b. KaptchaConfig(用于配置验证码样式,验证码规则等)
package com.gdss.ruralrevitalization.config;
import com.gdss.ruralrevitalization.config.fromYmlFile.KaptchaProps;
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;
//验证码生成器配置类
@Configuration
@EnableConfigurationProperties(KaptchaProps.class) //使KaptchaProps类生效
public class KaptchaConfig {
@Autowired
private KaptchaProps kaptchaProps;
@Bean
public DefaultKaptcha getDefaultKaptcha(){
DefaultKaptcha defaultKaptcha=new DefaultKaptcha();
Properties prop=new Properties();
//是否有边框,默认true,可自行设置yes/no
prop.setProperty(Constants.KAPTCHA_BORDER,"no");
//边框颜色,默认为black
prop.setProperty(Constants.KAPTCHA_BORDER_COLOR,"BLACK");
//验证码图片的宽度,默认200
prop.setProperty(Constants.KAPTCHA_IMAGE_WIDTH,kaptchaProps.getImageWidth());
//验证码图片的高度,默认50
prop.setProperty(Constants.KAPTCHA_IMAGE_HEIGHT,kaptchaProps.getImageHeight());
//文本字符间距,默认2
prop.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_SPACE,"3");
//文本字符大小,默认40
prop.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_SIZE, kaptchaProps.getFontSize());
//验证码长度,默认5
prop.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_LENGTH,"5");
//字体,默认Arial, Courier
prop.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_NAMES,"宋体,楷体,微软雅黑");
//设置干扰线颜色
prop.setProperty(Constants.KAPTCHA_NOISE_COLOR,kaptchaProps.getNoiseColor());
//设置文本实现类(如果使用默认的则不需要设置,我这里是用了自定义的 加减计算)
prop.setProperty(Constants.KAPTCHA_TEXTPRODUCER_IMPL,"com.test.lyy.common.imp.CalculationTextCreator");
Config config=new Config(prop);
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}
4. 编写CalculationTextCreator类,继承DefaultTextCreator(重写验证码生成规则)
import com.google.code.kaptcha.text.impl.DefaultTextCreator;
import java.util.Random;
//自定义Kaptcha的文本验证码,使用数学运算验证码
public class CalculationTextCreator extends DefaultTextCreator {
@Override
public String getText(){
//计算式的结果
StringBuilder text = new StringBuilder();
Random random = new Random();
int x = random.nextInt(100);
int y = random.nextInt(100);
//char[] operators={'+','-','*'};
int operatorRandom = (int) Math.round(Math.random());
//char operator=operators[operatorRandom];
switch (operatorRandom){
case 0:
text.append(x+"+"+y+"=?@"+(x+y));
break;
case 1:
if (x>y) text.append(x+"-"+y+"=?@"+(x-y));
else text.append(y+"-"+x+"=?@"+(y-x));
break;
}
return text.toString();
}
}
5.使用
@Autowired
DefaultKaptcha defaultKaptcha;
//获取验证码,并返回base64
@GetMapping("/getVerifyCode")
public String getVerifyCode() throws IOException {
String[] txt= defaultKaptcha.createText().split("@");
BufferedImage image=defaultKaptcha.createImage(txt[0]);
ByteArrayOutputStream outputStream=null;
outputStream=new ByteArrayOutputStream();
ImageIO.write(image,"jpg",outputStream);
//注意:JDK9 之后不支持这种写法,需要使用Base64.Encoder encoder=Base64.getEncoder();
BASE64Encoder encoder=new BASE64Encoder();
String base64=encoder.encode(outputStream.toByteArray());
String captchaBase64 = "data:image/jpeg;base64," + base64;
return captchaBase64.replace("\r\n","") ;
}
以下是出来的效果: