patchca验证码的使用
/**
* 生成验证码
*/
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验证码的使用的更多相关文章
- 【功能代码】---2.patchca生成验证码
Java使用patchca生成验证码 Patchca是Piotr Piastucki写的一个java验证码开源库,打包成jar文件发布,patchca使用简单但功能强大. 本例实现了自定 ...
- 【java提高】---patchca生成验证码
Java使用patchca生成验证码 Patchca是Piotr Piastucki写的一个java验证码开源库,打包成jar文件发布,patchca使用简单但功能强大. 本例实现了自定 ...
- patchca整合Spring MVC生成超炫的验证码
转载:http://lavasoft.blog.51cto.com/62575/1406947 @Controller public class Login2Controller { priv ...
- web开发(Java&Jquery)实现验证码
1. Ajax Fancy Capcha 一个支持 Ajax 又很炫的 jQuery Captcha 插件,它使用了很人性化的验证机制. from : http://www.webdesignbe ...
- kaptcha验证码使用
参数配置: Constant 描述 默认值 kaptcha.border 图片边框,合法值:yes , no yes kaptcha.border.color 边框颜色,合法值: r,g,b (and ...
- 图片验证码给AI使用
为了破解图形验证码,AI需要大量的图片数据.为了简单获取大量的图形来喂给Ai模型训练,索性自己写一把.代码来一发.. import java.awt.Color; import java.awt. ...
- javaweb登录验证码的实现
第一种 第一步: JSP <li><input name="validCode" id="validCode" type="te ...
- .net点选验证码实现思路分享
哈哈好久没冒泡了,最进看见点选验证码有点意思,所以想自己写一个. 先上效果图 如果你被这个效果吸引了就请继续看下去. 贴代码前先说点思路: 1.要有一个汉字库,并按字形分类.(我在数据库里是安部首分类 ...
- 【探索】无形验证码 —— PoW 算力验证
先来思考一个问题:如何写一个能消耗对方时间的程序? 消耗时间还不简单,休眠一下就可以了: Sleep(1000) 这确实消耗了时间,但并没有消耗 CPU.如果对方开了变速齿轮,这瞬间就能完成. 不过要 ...
随机推荐
- Ruby--学习记录(实时更新)
变量的命名方式决定了变量的种类: 局部变量 以英文字母或者_开头: 全局变量 以$开头: 实例变量 以@开头: 类变量 以@@开头:
- zookeeper 应用
一,启动一个zk客户端 zookeeper/bin/zkCli.sh ls / 查看根目录下所有节点 create /znode data 创建节点znode,并添加数据内容为data ls /z ...
- Lua 与 Redis
Lua 与 Redis 标签: Java与NoSQL 从 2.6版本 起, Redis 开始支持 Lua 脚本 让开发者自己扩展 Redis - 案例-实现访问频率限制: 实现访问者 $ip 在一定的 ...
- cf Gym 101086M ACPC Headquarters : AASTMT (Stairway to Heaven)
题目: Description standard input/output As most of you know, the Arab Academy for Science and Technolo ...
- [转]IE8兼容Object.keys
转自:http://blog.sina.com.cn/s/blog_6d63cf160102vbsg.html 只需要加入 var DONT_ENUM = "propertyIsEnumer ...
- Python’s SQLAlchemy vs Other ORMs[转发 2]Storm
Storm Storm is a Python ORM that maps objects between one or more databases and Python. It allows de ...
- Java中值传递和引用传递的概念
很多书中都提到了在Java中只存在值传递,但是今天在一个NanoHTTPD的源码中看到这样一段: if (qmi >= 0) { decodeParms(uri.substring(qmi + ...
- 中文Locale
sudo apt-get install locales dpkg-reconfigure locales 查看当前已安装locale: locale -a 查看locale设置: locale
- JBoss-7.1.1 http访问端口修改
修改http服务端口 找到 jboss-as-7.1.1.Final/standalone/configuration/standalone.xml文件,找到第298行,如下图: 如果我们想改成80端 ...
- nodeType的意思
nodeType是用来获得当前节点对象的类型.nodeType 属性可返回节点的类型.元素element 1 属性attr 2 文本text 3 注释comments 8 ...