A-A+
使用patchca生成验证码
patchca是一个验证码开源类库,使用它生成验证码的步骤如下。
1.在Maven的pom.xml中添加如下节点:
- <dependency>
- <groupId>com.github.bingoohuang</groupId>
- <artifactId>patchca</artifactId>
- <version>0.0.1</version>
- </dependency>
2.编写一个生成验证码的工具类,本文中命名其为“ValidatCodeUtils”,代码如下:
- public class ValidatCodeUtils {
- 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);
- } else {
- c[fi] = random.nextInt(256);
- }
- }
- return new Color(c[0], c[1], c[2]);
- }
- });
- cs.setBackgroundFactory(new BackgroundFactory() {
- public void fillBackground(BufferedImage bufferedImage) {
- Graphics graphics = bufferedImage.getGraphics();
- // 验证码图片的宽高
- int imgWidth = bufferedImage.getWidth();
- int imgHeight = bufferedImage.getHeight();
- // 画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);
- }
- }
- }
- });
- RandomWordFactory wf = new RandomWordFactory();
- wf.setCharacters("23456789abcdefghigkmnpqrstuvwxyzABCDEFGHIGKLMNPQRSTUVWXYZ");
- wf.setMaxLength(4);
- wf.setMinLength(4);
- cs.setWordFactory(wf);
- }
- public static String create(OutputStream os) 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;
- }
- return EncoderHelper.getChallangeAndWriteImage(cs, "png", os);
- }
- }
3.在controller中调用此工具类,并将其输出。
- @RequestMapping(value = "/vcode", method = RequestMethod.GET)
- public void getValidatCode(HttpServletRequest request, 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);
- try {
- String validatCode = ValidatCodeUtils.create(response.getOutputStream());
- } catch (Exception e) {
- }
- }
4.在前端HTML页面中访问上文中的地址,展示验证码图片。
- <div class="form-group has-feedback col-xs-4">
- <img src="${request.contextPath}/vcode.html" style="height: 34px" id="imgcode">
- </div>
5.最终的效果如下:
6.环境
SpringMVC4