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

CaptchaServiceSingleton类(单态类)

package com.dongbin.testy;

import com.octo.captcha.service.captchastore.FastHashMapCaptchaStore;
import com.octo.captcha.service.image.DefaultManageableImageCaptchaService;
import com.octo.captcha.service.image.ImageCaptchaService; public class CaptchaServiceSingleton { private static ImageCaptchaService instance =
new DefaultManageableImageCaptchaService( new FastHashMapCaptchaStore(), new ImageCaptchaEngineExtend(),
180, 100000, 75000); public static ImageCaptchaService getInstance(){
return instance;
}
}
ImageCaptchaEngineExtend类 (扩展验证码格式)

package com.dongbin.testy;

import java.awt.Color;
import java.awt.Font;
import com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator;
import com.octo.captcha.component.image.backgroundgenerator.UniColorBackgroundGenerator;
import com.octo.captcha.component.image.color.RandomRangeColorGenerator;
import com.octo.captcha.component.image.fontgenerator.FontGenerator;
import com.octo.captcha.component.image.fontgenerator.RandomFontGenerator;
import com.octo.captcha.component.image.textpaster.DecoratedRandomTextPaster;
import com.octo.captcha.component.image.textpaster.TextPaster;
import com.octo.captcha.component.image.textpaster.textdecorator.BaffleTextDecorator;
import com.octo.captcha.component.image.textpaster.textdecorator.LineTextDecorator;
import com.octo.captcha.component.image.textpaster.textdecorator.TextDecorator;
import com.octo.captcha.component.image.wordtoimage.ComposedWordToImage;
import com.octo.captcha.component.image.wordtoimage.WordToImage;
import com.octo.captcha.component.word.wordgenerator.RandomWordGenerator;
import com.octo.captcha.component.word.wordgenerator.WordGenerator;
import com.octo.captcha.engine.image.ListImageCaptchaEngine;
import com.octo.captcha.image.gimpy.GimpyFactory; public class ImageCaptchaEngineExtend extends ListImageCaptchaEngine{ @Override
protected void buildInitialFactories() {
int num = 5;//默认为5个 //设置内容
WordGenerator wgen = new RandomWordGenerator("abcdefghijklmnopqrstuvwxyz123456789");
//设置颜色
RandomRangeColorGenerator cgen = new RandomRangeColorGenerator(
new int[] { 100, 255 }, new int[] { 100, 255 },
new int[] { 100, 255 }); RandomRangeColorGenerator cgen1 = new RandomRangeColorGenerator(
new int[] { 0, 100 }, new int[] { 0, 100 },
new int[] { 0, 100 }); //设置背景色
BackgroundGenerator backgroundGenerator = new UniColorBackgroundGenerator(new Integer(80), new Integer(37),Color.WHITE);
//设置字体
Font[] fontsList = new Font[] { new Font("Arial", 0, 12),
new Font("Tahoma", 0, 12), new Font("Verdana", 0, 12), };
FontGenerator fontGenerator = new RandomFontGenerator(new Integer(26),
new Integer(26), fontsList);
// BaffleTextDecorator baffleTextDecorator = new BaffleTextDecorator(1,cgen1);//气泡干扰
LineTextDecorator lineTextDecorator = new LineTextDecorator(0,cgen1);//曲线干扰
TextDecorator[] textDecorators = new TextDecorator[1];
// textDecorators[0] = baffleTextDecorator;
textDecorators[0] = lineTextDecorator;
TextPaster textPaster = new DecoratedRandomTextPaster(new Integer(num),new Integer(num), cgen, true,textDecorators);
// TextPaster textPaster = new DecoratedRandomTextPaster(new Integer(num),new Integer(num), cgen, true,
// new TextDecorator[] { new BaffleTextDecorator(new Integer(1), Color.WHITE) });
WordToImage wordToImage = new ComposedWordToImage(fontGenerator,backgroundGenerator, textPaster);
this.addFactory(new GimpyFactory(wgen, wordToImage)); } }

ImageCaptchaServlet类

package com.dongbin.testy;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import javax.imageio.ImageIO;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.octo.captcha.service.CaptchaServiceException; public class ImageCaptchaServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void init(ServletConfig servletConfig) throws ServletException { super.init(servletConfig); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setHeader("Cache-Control", "no-store");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
ServletOutputStream out = response.getOutputStream();
try {
String captchaId = request.getSession(true).getId();
BufferedImage challenge = (BufferedImage) CaptchaServiceSingleton.getInstance().getChallengeForID(captchaId, request.getLocale());
ImageIO.write(challenge, "jpg", out);
out.flush();
} catch (CaptchaServiceException e) {
} finally {
out.close();
}
}
}

页面index.jsp

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

效果图

验证VerifyCodeServlet类

package com.dongbin.testy;

import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.octo.captcha.service.CaptchaServiceException; public class VerifyCodeServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doGet(req, resp);
} @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
boolean b = false; String codeValue =request.getParameter("j_captcha_response"); try {
b = CaptchaServiceSingleton.getInstance().validateResponseForID(request.getSession().getId(), codeValue.toLowerCase());
} catch (CaptchaServiceException e) {
e.printStackTrace();
} response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>验证结果:</TITLE></HEAD>");
out.println(" <BODY>");
if(b == Boolean.TRUE){
out.print("验证通过!");
}else {
out.print("验证失败!");
}
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close(); } }
												

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. [阿当视频]WEB组件学习笔记

    — 视频看完了,自定义事件还不懂,等完全懂了再更新并完成整篇案例 1. JS分层和组件的种类浏览器底层包括HTML CSS JS(DOM/BOM/Style/Canvas 2D/WebGl/SVG) ...

  2. Linux_jdk

    先查看下 yum list java* yum install java-1.7.0-openjdk* -y 环境变量应该是会自动配置的 或者手动配置编辑/etc/profile #vi /etc/p ...

  3. js 冒泡排序

    var arr = []; for(var i=0; i<100000; i++){ arr.push(parseInt(Math.random()*100)) }; var t1 = Date ...

  4. wcf测试工具

    WCF测试工具-WcfStorm WCF测试工具-WcfStorm  http://www.wcfstorm.com/wcf/home.aspx WcfStorm is a dead-simple, ...

  5. C#中值类型与引用类型通俗理解

    关于值类型和引用类型已经有很多人写了很多文章,但是很多人也只是停留在字面上的理解,如果采用一种通俗的方法来解释,想必很多人都会理解.我们都知道值类型存储在栈上,引用类型存储在堆上,引用类型都是xxx类 ...

  6. es6--(二)变量的解构赋值

    1.数组的解构赋值 //数组解析 let [a,b,c] = [1,2,3]; //a=1;b=2;c=3 //嵌套数组 let [a,[b,c]] = [1,[2,3]];//a=1;b=2;c=3 ...

  7. ftp服务器端的安装及配置

    搭建过程 安装 vsftp 服务(yum 安装即可) 配置/etc/vsftpd/vsftpd.conf   anonymous_enable=NO #禁止匿名登录 local_enable=YES ...

  8. flume从kafka读取数据到hdfs中的配置

    #source的名字 agent.sources = kafkaSource # channels的名字,建议按照type来命名 agent.channels = memoryChannel # si ...

  9. win10下安装Django

    Django的核心(1.4+)可以运行在从2.5到2.7之间的任何Python版本. 我的电脑是操作系统是window10 ,内存是4G. 1.下载django 官网地址:https://www.dj ...

  10. 《JS权威指南学习总结--第五章语句》

    内容要点: 一.throw语句     所谓异常是当发生了某种异常情况或错误时产生的一个信号.     抛出异常就是用信号通知发生了错误或异常状况.     捕获异常时指处理这个信号,即采取必要的手段 ...