spring boot 配置:
@Bean
public DefaultKaptcha getDefaultKaptcha(){
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
Properties properties = new Properties();
properties.setProperty("參數名", "參數值");
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
圖片生成到文件:
public void kaptcha(HttpServletRequest request, HttpServletResponse response) {
byte[] captchaChallengeAsJpeg = null;
ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
try {
//生產驗證碼字符串並保存到session中
String createText = kaptcha.createText();
request.getSession().setAttribute("vrifyCode", createText);
//使用生產的驗證碼字符串返回一個BufferedImage對象並轉爲byte寫入到byte數組中
BufferedImage challenge = kaptcha.createImage(createText);
ImageIO.write(challenge, "png", jpegOutputStream);
captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
/* 圖片保存
String imgSrc = "src/main/resources/static/img/kaptcha/kaptcha.png";
File file = new File(imgSrc);
if (file.exists() && file.getName().equals("kaptcha.png"))
file.delete();
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(captchaChallengeAsJpeg);
fileOutputStream.close();*///定義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");
ServletOutputStream responseOutputStream = response.getOutputStream();
responseOutputStream.write(captchaChallengeAsJpeg);
responseOutputStream.flush();
responseOutputStream.close();
}
catch (Exception e) {
e.printStackTrace();;
return;
}
}
驗證:
public boolean kaptchaValidate(HttpServletRequest request, HttpServletResponse response) {
String captchaId = (String) request.getSession().getAttribute("vrifyCode");
String parameter = request.getParameter("vrifyCode");
if (!captchaId.equals(parameter))
return false;
return true;
}
參數詳解:
Constant | 描述 | 默認值 |
kaptcha.border | 圖片邊框,合法值:yes , no | yes |
kaptcha.border.color | 邊框顏色,合法值: r,g,b (and optional alpha) 或者 white,black,blue. | black |
kaptcha.border.thickness | 邊框厚度,合法值:>0 | 1 |
kaptcha.image.width | 圖片寬 | 200 |
kaptcha.image.height | 圖片高 | 50 |
kaptcha.producer.impl | 圖片實現類 | com.google.code.kaptcha.impl.DefaultKaptcha |
kaptcha.textproducer.impl | 文本實現類 | com.google.code.kaptcha.text.impl.DefaultTextCreator |
kaptcha.textproducer.char.string | 文本集合,驗證碼值從此集合中獲取 | abcde2345678gfynmnpwx |
kaptcha.textproducer.char.length | 驗證碼長度 | 5 |
kaptcha.textproducer.font.names | 字體 | Arial, Courier |
kaptcha.textproducer.font.size | 字體大小 | 40px |
kaptcha.textproducer.font.color | 字體顏色,合法值: r,g,b 或者 white,black,blue. | black |
kaptcha.textproducer.char.space | 文字間隔 | 2 |
kaptcha.noise.impl | 干擾實現類 | com.google.code.kaptcha.impl.DefaultNoise |
kaptcha.noise.color | 干擾顏色,合法值: r,g,b 或者 white,black,blue. | black |
kaptcha.obscurificator.impl | 圖片樣式: 水紋com.google.code.kaptcha.impl.WaterRipple 魚眼com.google.code.kaptcha.impl.FishEyeGimpy 陰影com.google.code.kaptcha.impl.ShadowGimpy |
com.google.code.kaptcha.impl.WaterRipple |
kaptcha.background.impl | 背景實現類 | com.google.code.kaptcha.impl.DefaultBackground |
kaptcha.background.clear.from | 背景顏色漸變,開始顏色 | light grey |
kaptcha.background.clear.to | 背景顏色漸變,結束顏色 | white |
kaptcha.word.impl | 文字渲染器 | com.google.code.kaptcha.text.impl.DefaultWordRenderer |
kaptcha.session.key | session key | KAPTCHA_SESSION_KEY |
kaptcha.session.date | session date | KAPTCHA_SESSION_DATE |