JCaptcha 是一个用来生成验证码的开源Java类库

CaptchaServiceSingleton类(单态类)

  1. package com.dongbin.testy;
  2.  
  3. import com.octo.captcha.service.captchastore.FastHashMapCaptchaStore;
  4. import com.octo.captcha.service.image.DefaultManageableImageCaptchaService;
  5. import com.octo.captcha.service.image.ImageCaptchaService;
  6.  
  7. public class CaptchaServiceSingleton {
  8.  
  9. private static ImageCaptchaService instance =
  10. new DefaultManageableImageCaptchaService( new FastHashMapCaptchaStore(), new ImageCaptchaEngineExtend(),
  11. 180, 100000, 75000);
  12.  
  13. public static ImageCaptchaService getInstance(){
  14. return instance;
  15. }
  16. }
  1. ImageCaptchaEngineExtend (扩展验证码格式)
  1. package com.dongbin.testy;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator;
  6. import com.octo.captcha.component.image.backgroundgenerator.UniColorBackgroundGenerator;
  7. import com.octo.captcha.component.image.color.RandomRangeColorGenerator;
  8. import com.octo.captcha.component.image.fontgenerator.FontGenerator;
  9. import com.octo.captcha.component.image.fontgenerator.RandomFontGenerator;
  10. import com.octo.captcha.component.image.textpaster.DecoratedRandomTextPaster;
  11. import com.octo.captcha.component.image.textpaster.TextPaster;
  12. import com.octo.captcha.component.image.textpaster.textdecorator.BaffleTextDecorator;
  13. import com.octo.captcha.component.image.textpaster.textdecorator.LineTextDecorator;
  14. import com.octo.captcha.component.image.textpaster.textdecorator.TextDecorator;
  15. import com.octo.captcha.component.image.wordtoimage.ComposedWordToImage;
  16. import com.octo.captcha.component.image.wordtoimage.WordToImage;
  17. import com.octo.captcha.component.word.wordgenerator.RandomWordGenerator;
  18. import com.octo.captcha.component.word.wordgenerator.WordGenerator;
  19. import com.octo.captcha.engine.image.ListImageCaptchaEngine;
  20. import com.octo.captcha.image.gimpy.GimpyFactory;
  21.  
  22. public class ImageCaptchaEngineExtend extends ListImageCaptchaEngine{
  23.  
  24. @Override
  25. protected void buildInitialFactories() {
  26. int num = 5;//默认为5个
  27.  
  28. //设置内容
  29. WordGenerator wgen = new RandomWordGenerator("abcdefghijklmnopqrstuvwxyz123456789");
  30. //设置颜色
  31. RandomRangeColorGenerator cgen = new RandomRangeColorGenerator(
  32. new int[] { 100, 255 }, new int[] { 100, 255 },
  33. new int[] { 100, 255 });
  34.  
  35. RandomRangeColorGenerator cgen1 = new RandomRangeColorGenerator(
  36. new int[] { 0, 100 }, new int[] { 0, 100 },
  37. new int[] { 0, 100 });
  38.  
  39. //设置背景色
  40. BackgroundGenerator backgroundGenerator = new UniColorBackgroundGenerator(new Integer(80), new Integer(37),Color.WHITE);
  41. //设置字体
  42. Font[] fontsList = new Font[] { new Font("Arial", 0, 12),
  43. new Font("Tahoma", 0, 12), new Font("Verdana", 0, 12), };
  44. FontGenerator fontGenerator = new RandomFontGenerator(new Integer(26),
  45. new Integer(26), fontsList);
  46. // BaffleTextDecorator baffleTextDecorator = new BaffleTextDecorator(1,cgen1);//气泡干扰
  47. LineTextDecorator lineTextDecorator = new LineTextDecorator(0,cgen1);//曲线干扰
  48. TextDecorator[] textDecorators = new TextDecorator[1];
  49. // textDecorators[0] = baffleTextDecorator;
  50. textDecorators[0] = lineTextDecorator;
  51. TextPaster textPaster = new DecoratedRandomTextPaster(new Integer(num),new Integer(num), cgen, true,textDecorators);
  52. // TextPaster textPaster = new DecoratedRandomTextPaster(new Integer(num),new Integer(num), cgen, true,
  53. // new TextDecorator[] { new BaffleTextDecorator(new Integer(1), Color.WHITE) });
  54. WordToImage wordToImage = new ComposedWordToImage(fontGenerator,backgroundGenerator, textPaster);
  55. this.addFactory(new GimpyFactory(wgen, wordToImage));
  56.  
  57. }
  58.  
  59. }
  1.  

ImageCaptchaServlet类

  1. package com.dongbin.testy;
  2.  
  3. import java.awt.image.BufferedImage;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.IOException;
  6.  
  7. import javax.imageio.ImageIO;
  8. import javax.servlet.ServletConfig;
  9. import javax.servlet.ServletException;
  10. import javax.servlet.ServletOutputStream;
  11. import javax.servlet.http.HttpServlet;
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpServletResponse;
  14.  
  15. import com.octo.captcha.service.CaptchaServiceException;
  16.  
  17. public class ImageCaptchaServlet extends HttpServlet {
  18.  
  19. private static final long serialVersionUID = 1L;
  20.  
  21. public void init(ServletConfig servletConfig) throws ServletException {
  22.  
  23. super.init(servletConfig);
  24.  
  25. }
  26.  
  27. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  28.  
  29. response.setHeader("Cache-Control", "no-store");
  30. response.setHeader("Pragma", "no-cache");
  31. response.setDateHeader("Expires", 0);
  32. response.setContentType("image/jpeg");
  33. ServletOutputStream out = response.getOutputStream();
  34. try {
  35. String captchaId = request.getSession(true).getId();
  36. BufferedImage challenge = (BufferedImage) CaptchaServiceSingleton.getInstance().getChallengeForID(captchaId, request.getLocale());
  37. ImageIO.write(challenge, "jpg", out);
  38. out.flush();
  39. } catch (CaptchaServiceException e) {
  40. } finally {
  41. out.close();
  42. }
  43. }
  44. }

页面index.jsp

  1. <form action="testjcaptcha">
  2. <label for="j_captcha_response">请填写验证码:</label><br/>
  3. <img src="jcaptcha"alt="点击刷新" onClick="this.src='jcaptcha?t=' + Math.random();" align="absmiddle" style="vertical-align: bottom;margin: 10px 0 0 10px;cursor: pointer;">
  4. <input type="text" name="j_captcha_response" value="">
  5. <input type="submit" value="提交">
  6. </form>

效果图

验证VerifyCodeServlet类

  1. package com.dongbin.testy;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5.  
  6. import javax.servlet.ServletException;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10.  
  11. import com.octo.captcha.service.CaptchaServiceException;
  12.  
  13. public class VerifyCodeServlet extends HttpServlet {
  14.  
  15. private static final long serialVersionUID = 1L;
  16.  
  17. @Override
  18. protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  19. throws ServletException, IOException {
  20. this.doGet(req, resp);
  21. }
  22.  
  23. @Override
  24. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  25. throws ServletException, IOException {
  26. boolean b = false;
  27.  
  28. String codeValue =request.getParameter("j_captcha_response");
  29.  
  30. try {
  31. b = CaptchaServiceSingleton.getInstance().validateResponseForID(request.getSession().getId(), codeValue.toLowerCase());
  32. } catch (CaptchaServiceException e) {
  33. e.printStackTrace();
  34. }
  35.  
  36. response.setContentType("text/html;charset=utf-8");
  37. PrintWriter out = response.getWriter();
  38. out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
  39. out.println("<HTML>");
  40. out.println(" <HEAD><TITLE>验证结果:</TITLE></HEAD>");
  41. out.println(" <BODY>");
  42. if(b == Boolean.TRUE){
  43. out.print("验证通过!");
  44. }else {
  45. out.print("验证失败!");
  46. }
  47. out.println(" </BODY>");
  48. out.println("</HTML>");
  49. out.flush();
  50. out.close();
  51.  
  52. }
  53.  
  54. }
  1.  

captche验证码的更多相关文章

  1. .net点选验证码实现思路分享

    哈哈好久没冒泡了,最进看见点选验证码有点意思,所以想自己写一个. 先上效果图 如果你被这个效果吸引了就请继续看下去. 贴代码前先说点思路: 1.要有一个汉字库,并按字形分类.(我在数据库里是安部首分类 ...

  2. 【探索】无形验证码 —— PoW 算力验证

    先来思考一个问题:如何写一个能消耗对方时间的程序? 消耗时间还不简单,休眠一下就可以了: Sleep(1000) 这确实消耗了时间,但并没有消耗 CPU.如果对方开了变速齿轮,这瞬间就能完成. 不过要 ...

  3. TODO:Laravel增加验证码

    TODO:Laravel增加验证码1. 先聊聊验证码是什么,有什么作用?验证码(CAPTCHA)是"Completely Automated Public Turing test to te ...

  4. PHP-解析验证码类--学习笔记

    1.开始 在 网上看到使用PHP写的ValidateCode生成验证码码类,感觉不错,特拿来分析学习一下. 2.类图 3.验证码类部分代码 3.1  定义变量 //随机因子 private $char ...

  5. 随手记_C#验证码

    前言 最近在网上偶然看见一个验证码,觉得很有意思,于是搜了下,是使用第三方实现的,先看效果: 总体来说效果还是可以的,官方提供的SDK也比较详细,可配置性很高.在这里在简单啰嗦几句使用方式: 使用步骤 ...

  6. WPF做12306验证码点击效果

    一.效果 和12306是一样的,运行一张图上点击多个位置,横线以上和左边框还有有边框位置不允许点击,点击按钮输出坐标集合,也就是12306登陆的时候,需要向后台传递的参数. 二.实现思路 1.获取验证 ...

  7. 零OCR基础6行代码实现C#验证码识别

    这两天因为工作需要,要到某个网站采集信息,一是要模拟登陆,二是要破解验证码,本想用第三方付费打码,但是想想网上免费的代码也挺多的,于是乎准备从网上撸点代码下来,谁知道,撸了好多个都不行,本人以前也没接 ...

  8. ASP.NET中画图形验证码

    context.Response.ContentType = "image/jpeg"; //生成随机的中文验证码 string yzm = "人口手大小多少上中下男女天 ...

  9. asp.net mvc 验证码

    效果图 验证码类 namespace QJW.VerifyCode { //用法: //public FileContentResult CreateValidate() //{ // Validat ...

随机推荐

  1. 设计模式-GoF23

    书呢,是2012年双11买的. 没有面向对象程序经验的人,果然还是看不懂的.

  2. Jquery实现双击表单元格可编辑

    <script type="text/javascript"> function doTableTdEditClick(param){ doTdEditable(par ...

  3. centos7,yum安装的redis用systemctl无法启动

    因为之前使用显示命令启动redis的,要使redis在后台运行就需要改redis.conf中的daemonize 为yes. 这次在centos7上也顺手改了为yes,然后使用systemctl启动, ...

  4. poi做Excel数据驱动,支持.xls和.xlsx格式的excel文档,比起jxl强大不少

    import java.io.FileInputStream;import java.io.InputStream;import java.util.Iterator;import java.util ...

  5. 项目管理-SVN服务器的搭建

    Subversion是优秀的版本控制工具,其具体的的优点和详细介绍,这里就不再多说. 首先来下载和搭建SVN服务器. 现在Subversion已经迁移到apache网站上了,下载地址: http:// ...

  6. Unity3d中Dictionary和KeyValuePair的使用

    using UnityEngine; using System.Collections; using System.Collections.Generic;public class test : Mo ...

  7. ImageView.ScaleType设置图解

    图文相配很清晰的看出每个属性的效果, 感觉 CENTER_CROP 比较有用,长宽自动适应 ImageView ,整个图片自动缩略填充整个区域且居中显示(高宽不一定是view的尺寸),以前用JS在网页 ...

  8. jQuery获取当前对象标签名称

    获取当前对象标签名称 $(".classname")[0].tagName;

  9. hdu1010

    #include <stdio.h>#include <string.h>#include <math.h> int n,m,t;char map[10][10]; ...

  10. express学习点滴- 永远不要忘记异步

    直接上两段代码,因为nodejs基于异步和事件回调的解决方式,涉及到异步的时候,问题往往藏得很深,以下这个简单的问题困扰了很久.之前怀疑是各种问题,到处改.直到最后一步一步跟代码,跟操作数据库部分豁然 ...