服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java中使用patchca生成超炫的验证码

Java中使用patchca生成超炫的验证码

2020-11-24 14:57zsg88 Java教程

这篇文章主要介绍了Java中使用patchca生成超炫的验证码 的相关资料,需要的朋友可以参考下

官网:http://code.google.com/p/patchca/

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
   * 验证码
   *
   * @param width 图片宽度
   * @param height 图片高度
   * @param number 验证码数量
   */
  @RequestMapping(value = {"/captcha"})
  public void captcha(HttpServletRequest request,
            HttpServletResponse response, @RequestParam("w") int width, @RequestParam("h") int height, @RequestParam("n") int number) throws IOException {
    ConfigurableCaptchaService cs = new ConfigurableCaptchaService();
    cs.setColorFactory(new SingleColorFactory(new Color(25, 60, 170)));
    cs.setFilterFactory(new CurvesRippleFilterFactory(cs.getColorFactory()));
    RandomFontFactory ff = new RandomFontFactory();
    ff.setMinSize(30);
    ff.setMaxSize(30);
    RandomWordFactory rwf = new RandomWordFactory();
    rwf.setMinLength(number);
    rwf.setMaxLength(number);
    cs.setWordFactory(rwf);
    cs.setFontFactory(ff);
    cs.setHeight(height);
    cs.setWidth(width);
    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);
    ServletOutputStream stream = response.getOutputStream();
    String validate_code = EncoderHelper.getChallangeAndWriteImage(cs,
        "png", stream);
    Subject subject = SecurityUtils.getSubject();
    subject.getSession().setAttribute(formAuthenticationFilter.getCaptchaParam(), validate_code);
    stream.flush();
    stream.close();
  }

以上所述是小编给大家介绍的Java中使用patchca生成超炫的验证码 ,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

原文链接:http://blog.csdn.net/zsg88/article/details/73699271

延伸 · 阅读

精彩推荐