spring boot 图文验证码(Kaptcha)使用
如何使用
可以直接去官网http://code.google.com/p/kaptcha/下载jar,或者在pom.xml中导入
`
<groupId>com.baomidou</groupId>
<artifactId>kaptcha-spring-boot-starter</artifactId>
<version>1.1.0</version>
</dependency>
`
Kaptcha 详细配置表
业务代码
@Service
public class CaptchaService extends BaseService {
@Resource(name = "captchaProducerMath")
private Producer captchaProducerMath;
@Autowired
RedisOps redisOps;
/**
* 生成图片验证码
*/
public DataRespBean<Map<String, Object>> getPictureCaptcha(HttpServletResponse response) throws IOException {
String uuid = UUID.randomUUID().toString();
String capStr = null, code = null;
BufferedImage image = null;
// 生成验证码
String capText = captchaProducerMath.createText();
//截取结果
capStr = capText.substring(0, capText.lastIndexOf("@"));
code = capText.substring(capText.lastIndexOf("@") + 1);
image = captchaProducerMath.createImage(capStr);
//结果存入redis,key为uuid 超时时间5分钟
redisOps.set(RedisUtil.pictureCaptchaKey(uuid), code, 5 * 60); // 5分钟
// 转换流信息写出
FastByteArrayOutputStream outputStream= new FastByteArrayOutputStream();
Map<String, Object> map = new HashMap<>();
try {
ImageIO.write(image, "jpg", outputStream);
} catch (IOException e) {
return new DataRespBean(ResultCodeMsg.SERVER_ERROR.getErrMsg(), e.getMessage());
}finally {
map.put("uuid", uuid);
map.put("img", Base64Encoder.encode(outputStream.toByteArray()));
os.close();
}
return new DataRespBean<>(map);
}
}
验证码生成规则(根据实际需求修改)
public class KaptchaTextCreator extends DefaultTextCreator {
private static final String[] NUMBER= "0,1,2,3,4,5,6,7,8,9,10".split(",");
@Override
public String getText() {
Integer result = 0;//结果
Random random = new Random();
int x = random.nextInt(10);
int y = random.nextInt(10);
StringBuilder chinese = new StringBuilder();
int randomop = (int) random.nextInt(4)
//判断结果生成加减乘除
switch (randomoperands){
case 0 :
result = x * y;
chinese.append(NUMBER[x]);
chinese.append("*");
chinese.append(NUMBER[y]);
break;
case 1 :
if (!x == 0 && y % x == 0) {
result = y / x;
chinese.append(NUMBER[y]);
chinese.append("/");
chinese.append(NUMBER[x]);
} else {
result = x + y;
chinese.append(NUMBER[x]);
chinese.append("+");
chinese.append(NUMBER[y]);
}
break;
case 2 :
if (x >= y) {
result = x - y;
chinese.append(NUMBER[x]);
chinese.append("-");
chinese.append(NUMBER[y]);
} else {
result = y - x;
chinese.append(NUMBER[y]);
chinese.append("-");
chinese.append(NUMBER[x]);
}
break;
default:
result = x + y;
chinese.append(NUMBER[x]);
chinese.append("+");
chinese.append(NUMBER[y]);
}
//拼接结果返回
suChinese.append("=?@" + result);
return suChinese.toString();
}
生成图片验证码配置
@Configuration
public class CaptchaConfig {
@Bean(name = "captchaProducerMath")
public DefaultKaptcha getKaptchaBeanMath() {
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, "130");
// 图片高度
properties.setProperty(KAPTCHA_IMAGE_HEIGHT, "32");
// 文本字符大小
properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_SIZE, "28");
// KAPTCHA_SESSION_KEY
properties.setProperty(KAPTCHA_SESSION_CONFIG_KEY, "KAPTCHA_SESSION_KEY");
// 验证码文本生成器
properties.setProperty(KAPTCHA_TEXTPRODUCER_IMPL, "对应自己的KaptchaTextCreator 文件路径");
// 文本字符间距
properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_SPACE, "3");
// 文本字符长度
properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "6");
// 文本字体样式
properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Arial,Courier");
// 干扰颜色
properties.setProperty(KAPTCHA_NOISE_COLOR, "white");
// 干扰实现类
properties.setProperty(KAPTCHA_NOISE_IMPL, "com.google.code.kaptcha.impl.NoNoise");
// 图片样式
properties.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.ShadowGimpy");
Config config = new Config(properties);
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}
返回结果(jpg base64编码)
效果展示