/**
* 生成验证码
*/
private static RandomFontFactory ff = null;
// 自定义验证码图片背景
private static MyCustomBackgroundFactory backgroundFactory = new MyCustomBackgroundFactory();
private static ConfigurableCaptchaService cs = new ConfigurableCaptchaService();
private static Random random = new Random();
static {
cs.setColorFactory(new ColorFactory() {
public Color getColor(int x) {
int[] c = new int[3];
int i = random.nextInt(c.length);
for (int fi = 0; fi < c.length; fi++) {
if (fi == i) {
c[fi] = random.nextInt(71);// 71
} else {
c[fi] = random.nextInt(256);// 256
}
}
return new Color(c[0], c[1], c[2]);
}
});
ff = new RandomFontFactory();
RandomWordFactory wf = new RandomWordFactory();
wf.setCharacters("abcdefghigkmnpqrstuvwxyzABCDEFGHIGKLMNPQRSTUVWXYZ1234567890");
wf.setMaxLength(5);
wf.setMinLength(4);
// 该代码经过测试 在后台修改字体大小不能自适应,只能在前台页面修改验证码高度和宽度
//设置验证码的背景颜色
cs.setBackgroundFactory(backgroundFactory);
cs.setWordFactory(wf);
cs.setFontFactory(ff);
}

protected void setResponseHeaders(HttpServletResponse response) {
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);
}

/**
*
* 自定义验证码图片背景,主要画一些噪点和干扰线
*/
private static class MyCustomBackgroundFactory implements BackgroundFactory {
private Random random = new Random();

public void fillBackground(BufferedImage image) {
Graphics graphics = image.getGraphics();
// 验证码图片的宽高
int imgWidth = image.getWidth();
int imgHeight = image.getHeight();
// 填充为白色背景
graphics.setColor(Color.lightGray);
graphics.fillRect(0, 0, imgWidth, imgHeight);
// 画100个噪点(颜色及位置随机)
for (int i = 0; i < 100; i++) {
// 随机颜色
int rInt = random.nextInt(255);
int gInt = random.nextInt(255);
int bInt = random.nextInt(255);
graphics.setColor(new Color(rInt, gInt, bInt));
// 随机位置
int xInt = random.nextInt(imgWidth - 3);

int yInt = random.nextInt(imgHeight - 2);
// 随机旋转角度
int sAngleInt = random.nextInt(360);
int eAngleInt = random.nextInt(360);
// 随机大小
int wInt = random.nextInt(6);
int hInt = random.nextInt(6);
graphics.fillArc(xInt, yInt, wInt, hInt, sAngleInt, eAngleInt);
// 画5条干扰线
if (i % 20 == 0) {
int xInt2 = random.nextInt(imgWidth);
int yInt2 = random.nextInt(imgHeight);
graphics.drawLine(xInt, yInt, xInt2, yInt2);
}
}
}
}

@RequestMapping("/safecode.do")
public void safecode(HttpServletRequest request,
HttpServletResponse response) throws IOException {
switch (random.nextInt(5)) {
case 0:
cs.setFilterFactory(new CurvesRippleFilterFactory(cs
.getColorFactory()));
break;
case 1:
cs.setFilterFactory(new MarbleRippleFilterFactory());
break;
case 2:
cs.setFilterFactory(new DoubleRippleFilterFactory());
break;
case 3:
cs.setFilterFactory(new WobbleRippleFilterFactory());
break;
case 4:
cs.setFilterFactory(new DiffuseRippleFilterFactory());
break;
}
HttpSession session = request.getSession(false);
if (session == null) {
session = request.getSession();
}
setResponseHeaders(response);
String randomCode = EncoderHelper.getChallangeAndWriteImage(cs, "png",
response.getOutputStream());
session.setAttribute("randomCode", randomCode);
}

patchca验证码的使用的更多相关文章

  1. 【功能代码】---2.patchca生成验证码

    Java使用patchca生成验证码        Patchca是Piotr Piastucki写的一个java验证码开源库,打包成jar文件发布,patchca使用简单但功能强大. 本例实现了自定 ...

  2. 【java提高】---patchca生成验证码

    Java使用patchca生成验证码        Patchca是Piotr Piastucki写的一个java验证码开源库,打包成jar文件发布,patchca使用简单但功能强大. 本例实现了自定 ...

  3. patchca整合Spring MVC生成超炫的验证码

    转载:http://lavasoft.blog.51cto.com/62575/1406947 @Controller public class Login2Controller {     priv ...

  4. web开发(Java&Jquery)实现验证码

    1. Ajax Fancy Capcha 一个支持 Ajax 又很炫的 jQuery Captcha 插件,它使用了很人性化的验证机制. ​ from : http://www.webdesignbe ...

  5. kaptcha验证码使用

    参数配置: Constant 描述 默认值 kaptcha.border 图片边框,合法值:yes , no yes kaptcha.border.color 边框颜色,合法值: r,g,b (and ...

  6. 图片验证码给AI使用

    为了破解图形验证码,AI需要大量的图片数据.为了简单获取大量的图形来喂给Ai模型训练,索性自己写一把.代码来一发..   import java.awt.Color; import java.awt. ...

  7. javaweb登录验证码的实现

    第一种 第一步:  JSP <li><input name="validCode"  id="validCode" type="te ...

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

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

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

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

随机推荐

  1. 02.JavaScript 面向对象精要--函数

    在JavaScript中,函数其实也是对象.是函数不同于其他对象的特点是:函数存在一个被称为[[Call]]的内部属性.[[Call]]属性是函数独有的,表明该对象可以被执行.ECMAScript 定 ...

  2. ORACLE迁移votedisk,spfile以及OCRfile的方法

    在安装GUI时,创建了第一块ASM磁盘,命名为DATA1,上面存放了spfile文件,ocrfile文件,并且作为了vote盘.感觉名字和实际不符,容易搞混,所以想删除这个磁盘,直接删除会报错: OR ...

  3. maven + eclipse + tomcat热部署

    环境:eclipse  + tomcat  + maven + m2eclipse 解决这种问题有两种方案(方案二更好用,不想看方案一的人可以直接绕过). 方案一: 第一种方案摘自http://blo ...

  4. The ProgID of the WorkspaceName's workspace factory

    The ProgID of the WorkspaceName's workspace factory [C#]public stringWorkspaceFactoryProgID {get; se ...

  5. java根据标点英文分词

    最近学习java字符串部分,用正则表达式做了一个简单的统计单词出现次数的小程序,目前只能统计英文. 整个程序包括三个包,分别为output,run,wordcount wordCount包 执行单词统 ...

  6. nginx和tomcat的区别

    web上的server都叫web server,但是大家分工也有不同的. nginx常用做静态内容服务和代理服务器(不是你FQ那个代理),直面外来请求转发给后面的应用服务(tomcat,django什 ...

  7. 利用dmidecode 查看服务器的信息技巧

    Please refer to the link: http://www.laozuo.org/6682.html

  8. c# DataGridView 的一些属性设置,序号,合并头

      this.dataGridView1.DataSource = this.dISASTERBindingSource;             this.dataGridView1.Locatio ...

  9. PHP字符串处理函数

    /* * 确定字符串长度 * int strlen(string str) * 比较两个字符串 * 1.strcmp函数对两个字符串进行二进制安全的比较,并区分大小写 * int strcmp(str ...

  10. SQL基础语句(提升)

    1.复制表(只复制结构,源表名:a 新表名:b) select * into b from a where 1<>1 2.拷贝表 insert into b(a,b,c) select d ...