captche验证码
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验证码的更多相关文章
- .net点选验证码实现思路分享
哈哈好久没冒泡了,最进看见点选验证码有点意思,所以想自己写一个. 先上效果图 如果你被这个效果吸引了就请继续看下去. 贴代码前先说点思路: 1.要有一个汉字库,并按字形分类.(我在数据库里是安部首分类 ...
- 【探索】无形验证码 —— PoW 算力验证
先来思考一个问题:如何写一个能消耗对方时间的程序? 消耗时间还不简单,休眠一下就可以了: Sleep(1000) 这确实消耗了时间,但并没有消耗 CPU.如果对方开了变速齿轮,这瞬间就能完成. 不过要 ...
- TODO:Laravel增加验证码
TODO:Laravel增加验证码1. 先聊聊验证码是什么,有什么作用?验证码(CAPTCHA)是"Completely Automated Public Turing test to te ...
- PHP-解析验证码类--学习笔记
1.开始 在 网上看到使用PHP写的ValidateCode生成验证码码类,感觉不错,特拿来分析学习一下. 2.类图 3.验证码类部分代码 3.1 定义变量 //随机因子 private $char ...
- 随手记_C#验证码
前言 最近在网上偶然看见一个验证码,觉得很有意思,于是搜了下,是使用第三方实现的,先看效果: 总体来说效果还是可以的,官方提供的SDK也比较详细,可配置性很高.在这里在简单啰嗦几句使用方式: 使用步骤 ...
- WPF做12306验证码点击效果
一.效果 和12306是一样的,运行一张图上点击多个位置,横线以上和左边框还有有边框位置不允许点击,点击按钮输出坐标集合,也就是12306登陆的时候,需要向后台传递的参数. 二.实现思路 1.获取验证 ...
- 零OCR基础6行代码实现C#验证码识别
这两天因为工作需要,要到某个网站采集信息,一是要模拟登陆,二是要破解验证码,本想用第三方付费打码,但是想想网上免费的代码也挺多的,于是乎准备从网上撸点代码下来,谁知道,撸了好多个都不行,本人以前也没接 ...
- ASP.NET中画图形验证码
context.Response.ContentType = "image/jpeg"; //生成随机的中文验证码 string yzm = "人口手大小多少上中下男女天 ...
- asp.net mvc 验证码
效果图 验证码类 namespace QJW.VerifyCode { //用法: //public FileContentResult CreateValidate() //{ // Validat ...
随机推荐
- 设计模式-GoF23
书呢,是2012年双11买的. 没有面向对象程序经验的人,果然还是看不懂的.
- Jquery实现双击表单元格可编辑
<script type="text/javascript"> function doTableTdEditClick(param){ doTdEditable(par ...
- centos7,yum安装的redis用systemctl无法启动
因为之前使用显示命令启动redis的,要使redis在后台运行就需要改redis.conf中的daemonize 为yes. 这次在centos7上也顺手改了为yes,然后使用systemctl启动, ...
- poi做Excel数据驱动,支持.xls和.xlsx格式的excel文档,比起jxl强大不少
import java.io.FileInputStream;import java.io.InputStream;import java.util.Iterator;import java.util ...
- 项目管理-SVN服务器的搭建
Subversion是优秀的版本控制工具,其具体的的优点和详细介绍,这里就不再多说. 首先来下载和搭建SVN服务器. 现在Subversion已经迁移到apache网站上了,下载地址: http:// ...
- Unity3d中Dictionary和KeyValuePair的使用
using UnityEngine; using System.Collections; using System.Collections.Generic;public class test : Mo ...
- ImageView.ScaleType设置图解
图文相配很清晰的看出每个属性的效果, 感觉 CENTER_CROP 比较有用,长宽自动适应 ImageView ,整个图片自动缩略填充整个区域且居中显示(高宽不一定是view的尺寸),以前用JS在网页 ...
- jQuery获取当前对象标签名称
获取当前对象标签名称 $(".classname")[0].tagName;
- hdu1010
#include <stdio.h>#include <string.h>#include <math.h> int n,m,t;char map[10][10]; ...
- express学习点滴- 永远不要忘记异步
直接上两段代码,因为nodejs基于异步和事件回调的解决方式,涉及到异步的时候,问题往往藏得很深,以下这个简单的问题困扰了很久.之前怀疑是各种问题,到处改.直到最后一步一步跟代码,跟操作数据库部分豁然 ...