<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
1格式配置
package com.borntrust.oa.web.common.config;
import java.util.Properties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
@Component
public class KaptchaConfig {
@Bean
public DefaultKaptcha getDefaultKaptcha(){
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", "black");
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.space", "4");
properties.setProperty("kaptcha.textproducer.char.length", "4");
properties.setProperty("kaptcha.textproducer.font.names", "楷体");
properties.setProperty("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.ShadowGimpy");
properties.setProperty("kaptcha.textproducer.char.string", "123456789");
Config config = new Config(properties);
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}
2在需要用到的地方注入
@Autowired
private DefaultKaptcha defaultKaptcha;
//redis起配合作用
@Autowired
private RedisRepository redisRepository;
3验证码输出
@RequestMapping("/defaultKaptcha")
public BaseResponse<Map<String, Object>> defaultKaptcha(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) throws Exception{
Map<String, Object> map=new HashMap<>();
ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
try {
//生成验证码保存到redis中
String key=UUID.randomUUID().toString();
String createText = defaultKaptcha.createText();
redisRepository.setExpire(“OaSmsCode:”+key, createText, 300);
//使用生产的验证码字符串返回一个BufferedImage对象并转为byte写入到byte数组中
BufferedImage challenge = defaultKaptcha.createImage(createText);
ImageIO.write(challenge, “jpg”, jpegOutputStream);
String base64Img = Base64.byteArrayToBase64(jpegOutputStream.toByteArray());
httpServletResponse.setHeader(“Cache-Control”, “no-store”);
httpServletResponse.setHeader(“Pragma”, “no-cache”);
httpServletResponse.setDateHeader(“Expires”, 0);
httpServletResponse.setContentType(“image/jpeg”);
/* BASE64Encoder encoder = new BASE64Encoder();
String base64Img = encoder.encode();*/
map.put(“key”, key);
map.put(“Img”, base64Img);
return new BaseResponse<Map<String,Object>>(ApiResultEnum.SUCCESS.status, ApiResultEnum.SUCCESS.message, map);
} catch (IllegalArgumentException e) {
httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
return new BaseResponse<Map<String,Object>>(ApiResultEnum.FAIL.status, ApiResultEnum.FAIL.message);
}
//定义response输出类型为image/jpeg类型,使用response输出流输出图片的byte数组
/ServletOutputStream responseOutputStream =
httpServletResponse.getOutputStream();
responseOutputStream.write(captchaChallengeAsJpeg);
responseOutputStream.flush();
responseOutputStream.close();/
}