Jfinal验证码功能
//验证码工具类
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.security.MessageDigest;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import com.jfinal.core.Controller;
import com.jfinal.kit.StringKit;
import com.jfinal.render.Render;
public class MyCaptchaRender extends Render {
private static final long serialVersionUID = -7599510915228560611L;
private static final String[] strArr = {"3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y"};
private static String randomCodeKey = "JFINAL_JLHHWH_Key";
private static boolean caseInsensitive = true;
private int img_width = 85;
private int img_height = 20;
private int img_randNumber = 6;
public MyCaptchaRender() {
}
public MyCaptchaRender(String randomKey) {
if (StringKit.isBlank(randomKey))
throw new IllegalArgumentException("randomKey can not be blank");
randomCodeKey = randomKey;
}
public MyCaptchaRender(int width, int height, int count, boolean isCaseInsensitive) {
if(width <=0 || height <=0 || count <=0)
{
throw new IllegalArgumentException("Image width or height or count must be > 0");
}
this.img_width = width;
this.img_height = height;
this.img_randNumber = count;
caseInsensitive = isCaseInsensitive;
}
public MyCaptchaRender(String randomKey,int width, int height, int count, boolean isCaseInsensitive) {
if (StringKit.isBlank(randomKey))
throw new IllegalArgumentException("randomKey can not be blank");
randomCodeKey = randomKey;
if(width <=0 || height <=0 || count <=0)
{
throw new IllegalArgumentException("Image width or height or count must be > 0");
}
this.img_width = width;
this.img_height = height;
this.img_randNumber = count;
caseInsensitive = isCaseInsensitive;
}
public void render() {
BufferedImage image = new BufferedImage(img_width, img_height, BufferedImage.TYPE_INT_RGB);
String vCode = drawGraphic(image);
vCode = encrypt(vCode);
Cookie cookie = new Cookie(randomCodeKey, vCode);
cookie.setMaxAge(-1);
cookie.setPath("/");
response.addCookie(cookie);
response.setHeader("Pragma","no-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
ServletOutputStream sos = null;
try {
sos = response.getOutputStream();
ImageIO.write(image, "jpeg",sos);
} catch (Exception e) {
throw new RuntimeException(e);
}
finally {
if (sos != null)
try {sos.close();} catch (IOException e) {e.printStackTrace();}
}
}
private String drawGraphic(BufferedImage image){
// 获取图形上下文
Graphics g = image.createGraphics();
// 生成随机类
Random random = new Random();
// 设定背景色
g.setColor(getRandColor(200,250));
g.fillRect(0, 0, img_width, img_height);
// 设定字体
g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
// 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
g.setColor(getRandColor(160, 200));
for (int i = 0; i < 155; i++) {
int x = random.nextInt(img_width);
int y = random.nextInt(img_height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
}
// 取随机产生的认证码(img_randNumber位数字)
String sRand = "";
for (int i = 0; i < img_randNumber; i++) {
String rand = String.valueOf(strArr[random.nextInt(strArr.length)]);
sRand += rand;
// 将认证码显示到图象中
g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
// 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
g.drawString(rand, 13 * i + 6, 16);
}
// 图象生效
g.dispose();
return sRand;
}
/*
* 给定范围获得随机颜色
*/
private Color getRandColor(int fc, int bc) {
Random random = new Random();
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
private static final String encrypt(String srcStr) {
try {
String result = "";
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] bytes = md.digest(srcStr.getBytes("utf-8"));
for(byte b:bytes){
String hex = Integer.toHexString(b&0xFF).toUpperCase();
result += ((hex.length() ==1 ) ? "0" : "") + hex;
}
return result;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static boolean validate(Controller controller, String inputRandomCode) {
if (StringKit.isBlank(inputRandomCode))
return false;
try {
if(caseInsensitive)
inputRandomCode = inputRandomCode.toUpperCase();
inputRandomCode = encrypt(inputRandomCode);
return inputRandomCode.equals(controller.getCookie(randomCodeKey));
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
//Controller类(调用该方法来产生一个新的验证码)
public class CaptchaController extends Controller {
/**
* @Title: index
* @Description: TODO(产生一个验证码)
* void
*/
public void index(){
render(new MyCaptchaRender(60,22,4,true));
}
//路由配置
public class FrontRoutes extends Routes
{
public void config()
{
add("/captcha",CaptchaController.class);
}
//页面代码
<body>
<img src="${path!}/captcha/index" id="checkCode" style="cursor: pointer;height: 70%;width: 80%;" title="点击刷新验证码" />
</body>
<script type="text/javascript">
//点击更换验证码
$(function(){
$("#checkCode").click(function(){
$(this).attr("src","${path!}/captcha?d="+Math.random().toString());
});
});
</script>
//效果图
Jfinal验证码功能的更多相关文章
- dd——留言板再加验证码功能
1.找到后台-核心-频道模型-自定义表单 2.然后点击增加新的自定义表单 diyid 这个,不管他,默认就好 自定义表单名称 这个的话,比如你要加个留言板还是投诉建议?写上去呗 数据表 这个不要碰, ...
- .Net Core 之 图形验证码 本文介绍.Net Core下用第三方ZKWeb.System.Drawing实现验证码功能。
本文介绍.Net Core下用第三方ZKWeb.System.Drawing实现验证码功能. 通过测试的系统: Windows 8.1 64bit Ubuntu Server 16.04 LTS 64 ...
- c#实现验证码功能
一.验证码简介 验证码功能一般是用于防止批量注册的,不少网站为了防止用户利用机器人自动注册.登录.灌水,都采用了验证码技术.所谓验证码,就是将一串随机产生的数字或字母或符号或文字,生成一幅图片, 图片 ...
- javaweb实现验证码功能
在javaweb的用户注册与登陆功能时,有时为了防止漏洞或者大量注册,可以使用验证码功能,下面是验证码的一个简单实现 验证码类 public class ValiImg extends HttpSer ...
- 使用JS来实现验证码功能
最近想为自己的Django博客添加验证码功能,本来想使用第三方库来实现的,不过考虑到添加第三方库对性能的影响,以及第三方库是否安全可靠的问题,还是用自己的代码来实现吧.反正用JS来实现验证码功能又不是 ...
- 用PHP实现验证码功能
目前,不少网站为了防止用户利用机器人自动注册.登录.灌水,都采用了 验证码技术.所谓验证码,就是将一串随机产生的数字或符号,生成一幅图片, 图片里加上一些干扰象素(防止OCR),由用户肉眼识别其中的验 ...
- ThinkPhp框架:验证码功能
Think\Verify类可以支持验证码的生成和验证功能. 为了显示这个验证码功能,第一要有控制器,再就是有方法,然后是显示的页面. 一.最简单的方式生成验证码 (1)我们还是继续在那个控制器编写方法 ...
- 纯JS实现图片验证码功能并兼容IE6-8
最近要搞一个图片验证码功能,但是又不想自己写后台代码.于是自己准备搞一个纯前端的验证码功能,于是网上搜索了一下,找到一个插件gVerify.js,简单好用,实现完美.不过后面接到说要兼容IE8,想想也 ...
- 用java来实现验证码功能(本帖为转载贴),作为个人学习收藏用
一.关于为何使用验证的解释 在目前的网页的登录.注册中经常会见到各种验证码.其目的便是为了:防止暴力破解 .因为只要CPU性能较强,便可以在慢慢尝试密码的过程中来破解用户账号,因而导致的结果是用户信 ...
随机推荐
- [Java] arraycopy 数组复制(转)
public class ArraycopyTest { public static void main(String[] args) { //静态初始化两个长度不同的 ...
- C# 编码转换 UTF8转GB2312 GB2312转UTF8
/// <summary> /// GB2312转换成UTF8 /// </summary> /// <param name="text">&l ...
- POJ 1741 Tree 树分治
Tree Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...
- 【rqnoj378】 约会计划
题目描述 cc是个超级帅哥,口才又好,rp极高(这句话似乎降rp),又非常的幽默,所以很多mm都跟他关系不错.然而,最关键的是,cc能够很好的调解各各妹妹间的关系.mm之间的关系及其复杂,cc必须严格 ...
- 数字信号处理实验(一)——DTFT
1.MATLAB自编绘图函数 function [] = signal_write(X,w,flag) % X:数据 % w:频率向量 magX=abs(X);angX=angle(X); realX ...
- 初探YAML
YAML何许物也?在XML泛滥的情况下,YAML的出现的确让人眼前一亮,在初步学习了YAML以后,粗略的总结了一下,拿出来和大家分享.[MindMap][参考文档]YAML Specification ...
- asp.net权限控制配置web.config
项目下 有三个文件夹 A,B,C 验正方式是 Forms 验正 我要设置他们的访问权限为, A,匿名可访问 B,普通用户授权后才能访问 C,只允许管理员访问 <configuration> ...
- POJ 3261 后缀数组
题目链接:http://poj.org/problem?id=3261 题意:约翰注意到奶牛产奶的之类是不断变化的,虽然他不能预测从当天到下一天的变化情况但是他知道变化是有规律的,牛奶的质量由一个整数 ...
- 提取Windows用户密钥文件cachedump
提取Windows用户密钥文件cachedump Windows系统将用户信息和密钥存储在系统存档文件(System hive)和安全存档(Security hive)中.只要提取这些内容,就可以 ...
- 我的c++学习(1)hello world!
// texthello.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> using na ...