patchca驗證碼的使用
/**
* 生成驗證碼
*/
private static RandomFontFactory ff = null;
// 自定義驗證碼圖片背景
private static MyCustomBackgroundFactory backgroundFactory = new MyCustomBackgroundFactory();
private static ConfigurableCaptchaService cs = new ConfigurableCaptchaService();
private static Random random = new Random();
static {
cs.setColorFactory(new ColorFactory() {
public Color getColor(int x) {
int[] c = new int[3];
int i = random.nextInt(c.length);
for (int fi = 0; fi < c.length; fi++) {
if (fi == i) {
c[fi] = random.nextInt(71);// 71
} else {
c[fi] = random.nextInt(256);// 256
}
}
return new Color(c[0], c[1], c[2]);
}
});
ff = new RandomFontFactory();
RandomWordFactory wf = new RandomWordFactory();
wf.setCharacters("abcdefghigkmnpqrstuvwxyzABCDEFGHIGKLMNPQRSTUVWXYZ1234567890");
wf.setMaxLength(5);
wf.setMinLength(4);
// 該代碼經過測試 在后臺修改字體大小不能自適應,只能在前臺頁面修改驗證碼高度和寬度
//設置驗證碼的背景顏色
cs.setBackgroundFactory(backgroundFactory);
cs.setWordFactory(wf);
cs.setFontFactory(ff);
}
protected void setResponseHeaders(HttpServletResponse response) {
response.setContentType("image/png");
response.setHeader("Cache-Control", "no-cache, no-store");
response.setHeader("Pragma", "no-cache");
long time = System.currentTimeMillis();
response.setDateHeader("Last-Modified", time);
response.setDateHeader("Date", time);
response.setDateHeader("Expires", time);
}
/**
*
* 自定義驗證碼圖片背景,主要畫一些噪點和干擾線
*/
private static class MyCustomBackgroundFactory implements BackgroundFactory {
private Random random = new Random();
public void fillBackground(BufferedImage image) {
Graphics graphics = image.getGraphics();
// 驗證碼圖片的寬高
int imgWidth = image.getWidth();
int imgHeight = image.getHeight();
// 填充為白色背景
graphics.setColor(Color.lightGray);
graphics.fillRect(0, 0, imgWidth, imgHeight);
// 畫100個噪點(顏色及位置隨機)
for (int i = 0; i < 100; i++) {
// 隨機顏色
int rInt = random.nextInt(255);
int gInt = random.nextInt(255);
int bInt = random.nextInt(255);
graphics.setColor(new Color(rInt, gInt, bInt));
// 隨機位置
int xInt = random.nextInt(imgWidth - 3);
int yInt = random.nextInt(imgHeight - 2);
// 隨機旋轉角度
int sAngleInt = random.nextInt(360);
int eAngleInt = random.nextInt(360);
// 隨機大小
int wInt = random.nextInt(6);
int hInt = random.nextInt(6);
graphics.fillArc(xInt, yInt, wInt, hInt, sAngleInt, eAngleInt);
// 畫5條干擾線
if (i % 20 == 0) {
int xInt2 = random.nextInt(imgWidth);
int yInt2 = random.nextInt(imgHeight);
graphics.drawLine(xInt, yInt, xInt2, yInt2);
}
}
}
}
@RequestMapping("/safecode.do")
public void safecode(HttpServletRequest request,
HttpServletResponse response) throws IOException {
switch (random.nextInt(5)) {
case 0:
cs.setFilterFactory(new CurvesRippleFilterFactory(cs
.getColorFactory()));
break;
case 1:
cs.setFilterFactory(new MarbleRippleFilterFactory());
break;
case 2:
cs.setFilterFactory(new DoubleRippleFilterFactory());
break;
case 3:
cs.setFilterFactory(new WobbleRippleFilterFactory());
break;
case 4:
cs.setFilterFactory(new DiffuseRippleFilterFactory());
break;
}
HttpSession session = request.getSession(false);
if (session == null) {
session = request.getSession();
}
setResponseHeaders(response);
String randomCode = EncoderHelper.getChallangeAndWriteImage(cs, "png",
response.getOutputStream());
session.setAttribute("randomCode", randomCode);
}
浙公網安備 33010602011771號