/**
* 生成验证码
*/
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. k.APR通道特殊配置

    APR/native specific configuration The following attributes are specific to the APR/native connector. ...

  2. 关于《hibernate多对多》有中间表的建立

    角色 与 菜单(资源)的多对多关系,在这里我们建立中间表,用两个oneToMany实现 实体类: 角色(GmRole)  菜单(GmMenu) 中间表(GmRoleRight) 1.在角色实体中 pa ...

  3. [sqoop1.99.6] 基于1.99.6版本的一个小例子

    1.创建mysql数据库.表.以及测试数据mysql> desc test;+-------+-------------+------+-----+---------+------------- ...

  4. c#数据绑定(4)——向查询中添加参数

    本实例主要练习了ADO.Net 连接到外部数据库的基础上,向查询中添加参数.使用的是ACCESS数据库. 在ACCESS数据库中可以用MSSQL的形式定义操作字符串,也可以采用OLEDB的形式. MS ...

  5. css学习归纳总结(三) 转

    原文地址:css学习归纳总结(三) 为文档添加样式的三种方法 行内样式 行内样式是写在HTML标签的style属性里的,比如: <p style="font-size: 12px;fo ...

  6. H5一行显示两个正方形

    1)有时候一些图片会是正方形或者长方形,对于这样的图片一般都是居中显示到正方体内,代码如下:  .exhibition_list img{width:100%;position: relative;t ...

  7. Post提交

    以下两种Post提交方法都可行 /// <summary> /// post 数据 /// </summary> /// <param name="url&qu ...

  8. Objective-C( Foundation框架 一 NSFileManager)

    NSFileManager 用来管理文件系统的 它可以用于常见的文件,文件夹操作(拷贝,剪切,创建) NSFileManager使用了单例模式(Singleton) 使用defaultManager可 ...

  9. MySql创建树结构递归查询存储过程

    在实现F2工作流底层多数据库支持时发现Oracel和mssql都有提供递归子查询,而MySql却没有,没办法需要自己构建存储过程来提供这个递归子查询的功能. -- 当前节点及子节点 -- 参数说明:i ...

  10. !important使用

    IE 6.0一直都不支持这个语法,而其他的浏览器都支持.因此我们就可以利用这一点来分别 给IE和其他浏览器不同的样式定义,例如,我们定义这样一个样式: colortest {border:20px s ...