利用patchca生成验证码

你的名字 2022-09-30 00:55 218阅读 0赞
  1. patchca 是一个简单但功能强大的验证码的Java类库。
  2. springMVC 中的使用
  3. /**
  4. * 验证码
  5. *
  6. * @param width 图片宽度
  7. * @param height 图片高度
  8. * @param number 验证码数量
  9. */
  10. @RequestMapping(value = {"/captcha?w={width}&h={height}&n={number}"})
  11. public void captcha(HttpServletRequest request,
  12. HttpServletResponse response, @PathVariable int width, @PathVariable int height, @PathVariable int number) throws IOException {
  13. ConfigurableCaptchaService cs = new ConfigurableCaptchaService();
  14. cs.setColorFactory(new SingleColorFactory(new Color(25, 60, 170)));
  15. cs.setFilterFactory(new CurvesRippleFilterFactory(cs.getColorFactory()));
  16. RandomFontFactory ff = new RandomFontFactory();
  17. ff.setMinSize(30);
  18. ff.setMaxSize(30);
  19. RandomWordFactory rwf = new RandomWordFactory();
  20. rwf.setMinLength(number);
  21. rwf.setMaxLength(number);
  22. cs.setWordFactory(rwf);
  23. cs.setFontFactory(ff);
  24. cs.setHeight(height);
  25. cs.setWidth(width);
  26. response.setContentType("image/png");
  27. response.setHeader("Cache-Control", "no-cache, no-store");
  28. response.setHeader("Pragma", "no-cache");
  29. long time = System.currentTimeMillis();
  30. response.setDateHeader("Last-Modified", time);
  31. response.setDateHeader("Date", time);
  32. response.setDateHeader("Expires", time);
  33. ServletOutputStream stream = response.getOutputStream();
  34. String validate_code = EncoderHelper.getChallangeAndWriteImage(cs,
  35. "png", stream);
  36. request.getSession().setAttribute("REG_VAL_CODE", validate_code);
  37. stream.flush();
  38. stream.close();
  39. }
  40. jar下载地址:http://code.google.com/p/patchca/downloads/list
  41. 源码下载地址:https://github.com/pusuo/patchca

发表评论

表情:
评论列表 (有 0 条评论,218人围观)

还没有评论,来说两句吧...

相关阅读

    相关 利用java自动生成验证

            验证码生成有两种形式,一种是保存一定的验证码图片在数据库中然后把相应的验证码也保存起来。通过查询数据库来确定用户输入的验证码是否正确。但是这种方式有很大的问题,