java实现动态验证码生成-mile米乐体育
本文实例为大家分享了java动态验证码生成的具体代码,供大家参考,具体内容如下
说明:今天给大家来带来一个自动生成验证码的处理方法。验证码的出现有效减少了注入灌水以及破解密码等恶意操作,提高了系统运行的流畅性,保护了系统用户的隐私安全,具体实现方法如下:
1.首先我们先编写一个专门的验证码生成工具类,该工具类代码如下:
package com.ordering.util; import java.awt.color; import java.awt.font; import java.awt.graphics; import java.awt.graphics2d; import java.awt.image.bufferedimage; import java.util.random; /** * 验证码生成器 * @author 诺坎普10号 * @date 2020-2-25 */ public class cpachautil { /** * 验证码来源 */ final private char[] code = { '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' }; /** * 字体 */ final private string[] fontnames = new string[]{ "黑体", "宋体", "courier", "arial", "verdana", "times", "tahoma", "georgia"}; /** * 字体样式 */ final private int[] fontstyles = new int[]{ font.bold, font.italic|font.bold }; /** * 验证码长度 * 默认4个字符 */ private int vcodelen = 4; /** * 验证码图片字体大小 * 默认17 */ private int fontsize = 21; /** * 验证码图片宽度 */ private int width = (fontsize 1)*vcodelen 10; /** * 验证码图片高度 */ private int height = fontsize 12; /** * 干扰线条数 * 默认3条 */ private int disturbline = 3; public cpachautil(){} /** * 指定验证码长度 * @param vcodelen 验证码长度 */ public cpachautil(int vcodelen) { this.vcodelen = vcodelen; this.width = (fontsize 1)*vcodelen 10; } /** * 指定验证码长度、图片宽度、高度 * @param vcodelen * @param width * @param height */ public cpachautil(int vcodelen,int width,int height) { this.vcodelen = vcodelen; this.width = width; this.height = height; } /** * 生成验证码图片 * @param vcode 要画的验证码 * @param drawline 是否画干扰线 * @return */ public bufferedimage generatorvcodeimage(string vcode, boolean drawline){ //创建验证码图片 bufferedimage vcodeimage = new bufferedimage(width, height, bufferedimage.type_int_rgb); graphics g = vcodeimage.getgraphics(); //填充背景色 g.setcolor(new color(246, 240, 250)); g.fillrect(0, 0, width, height); if(drawline){ drawdisturbline(g); } //用于生成伪随机数 random ran = new random(); //在图片上画验证码 for(int i = 0;i < vcode.length();i ){ //设置字体 g.setfont(new font(fontnames[ran.nextint(fontnames.length)], fontstyles[ran.nextint(fontstyles.length)], fontsize)); //随机生成颜色 g.setcolor(getrandomcolor()); //画验证码 g.drawstring(vcode.charat(i) "", i*fontsize 10, fontsize 5); } //释放此图形的上下文以及它使用的所有系统资源 g.dispose(); return vcodeimage; } /** * 获得旋转字体的验证码图片 * @param vcode * @param drawline 是否画干扰线 * @return */ public bufferedimage generatorrotatevcodeimage(string vcode, boolean drawline){ //创建验证码图片 bufferedimage rotatevcodeimage = new bufferedimage(width, height, bufferedimage.type_int_rgb); graphics2d g2d = rotatevcodeimage.creategraphics(); //填充背景色 g2d.setcolor(new color(246, 240, 250)); g2d.fillrect(0, 0, width, height); if(drawline){ drawdisturbline(g2d); } //在图片上画验证码 for(int i = 0;i < vcode.length();i ){ bufferedimage rotateimage = getrotateimage(vcode.charat(i)); g2d.drawimage(rotateimage, null, (int) (this.height * 0.7) * i, 0); } g2d.dispose(); return rotatevcodeimage; } /** * 生成验证码 * @return 验证码 */ public string generatorvcode(){ int len = code.length; random ran = new random(); stringbuffer sb = new stringbuffer(); for(int i = 0;i < vcodelen;i ){ int index = ran.nextint(len); sb.append(code[index]); } return sb.tostring(); } /** * 为验证码图片画一些干扰线 * @param g */ private void drawdisturbline(graphics g){ random ran = new random(); for(int i = 0;i < disturbline;i ){ int x1 = ran.nextint(width); int y1 = ran.nextint(height); int x2 = ran.nextint(width); int y2 = ran.nextint(height); g.setcolor(getrandomcolor()); //画干扰线 g.drawline(x1, y1, x2, y2); } } /** * 获取一张旋转的图片 * @param c 要画的字符 * @return */ private bufferedimage getrotateimage(char c){ bufferedimage rotateimage = new bufferedimage(height, height, bufferedimage.type_int_argb); graphics2d g2d = rotateimage.creategraphics(); //设置透明度为0 g2d.setcolor(new color(255, 255, 255, 0)); g2d.fillrect(0, 0, height, height); random ran = new random(); g2d.setfont(new font(fontnames[ran.nextint(fontnames.length)], fontstyles[ran.nextint(fontstyles.length)], fontsize)); g2d.setcolor(getrandomcolor()); double theta = gettheta(); //旋转图片 g2d.rotate(theta, height/2, height/2); g2d.drawstring(character.tostring(c), (height-fontsize)/2, fontsize 5); g2d.dispose(); return rotateimage; } /** * @return 返回一个随机颜色 */ private color getrandomcolor(){ random ran = new random(); return new color(ran.nextint(220), ran.nextint(220), ran.nextint(220)); } /** * @return 角度 */ private double gettheta(){ return ((int) (math.random()*1000) % 2 == 0 ? -1 : 1)*math.random(); } /** * @return 验证码字符个数 */ public int getvcodelen() { return vcodelen; } /** * 设置验证码字符个数 * @param vcodelen */ public void setvcodelen(int vcodelen) { this.width = (fontsize 3)*vcodelen 10; this.vcodelen = vcodelen; } /** * @return 字体大小 */ public int getfontsize() { return fontsize; } /** * 设置字体大小 * @param fontsize */ public void setfontsize(int fontsize) { this.width = (fontsize 3)*vcodelen 10; this.height = fontsize 15; this.fontsize = fontsize; } /** * @return 图片宽度 */ public int getwidth() { return width; } /** * 设置图片宽度 * @param width */ public void setwidth(int width) { this.width = width; } /** * @return 图片高度 */ public int getheight() { return height; } /** * 设置图片高度 * @param height */ public void setheight(int height) { this.height = height; } /** * @return 干扰线条数 */ public int getdisturbline() { return disturbline; } /** * 设置干扰线条数 * @param disturbline */ public void setdisturbline(int disturbline) { this.disturbline = disturbline; } }
2.然后在页面端定义一个显示验证码的控件,再写一个js函数来对后台动态生成验证码方法进行调用,主要代码如下:
3.控制器中代码如下:
/** * 本系统所有的验证码均采用此方法 * * @param vcodelen * @param width * @param height * @param cpachatype:用来区别验证码的类型,传入字符串 * @param request * @param response */ @requestmapping(value = "/get_cpacha", method = requestmethod.get) public void generatecpacha(@requestparam(name = "vl", required = false, defaultvalue = "4") integer vcodelen, @requestparam(name = "w", required = false, defaultvalue = "100") integer width, @requestparam(name = "h", required = false, defaultvalue = "30") integer height, @requestparam(name = "type", required = true, defaultvalue = "logincpacha") string cpachatype, httpservletrequest request, httpservletresponse response) { //根据页面传入的验证码长度、图片宽度以及图片长度来实例化验证码实体 cpachautil cpachautil = new cpachautil(vcodelen, width, height); //生成验证码 string generatorvcode = cpachautil.generatorvcode(); //把验证码存入session request.getsession().setattribute(cpachatype, generatorvcode); //根据验证码生成相应的验证码图片 bufferedimage generatorrotatevcodeimage = cpachautil.generatorrotatevcodeimaiymrxaeoge(generatorvcode, true); try { imageio.write(generatorrotatevcodeimage, "gif", response.getoutputstream()); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } }
4.实现效果如下图所示:
5.控制器验证码验证部分如下图所示:
//若验证码为空 if (stringutils.isempty(cpacha)) { ret.put("type", "error"); ret.put("msg", "请填写验证码!"); return ret; } //获取保存在session中的登录验证码对象 object logincpacha = request.getsession().getattribute("logincpacha"); //若登录验证码实体对象为空 if (logincpacha == null) { ret.put("type", "error"); ret.put("msg", "会话超时,请刷新页面!"); return ret; } //若输入的验证码转化成大写之后不等于转换成大写的session中保存的验证码 if (!cpacha.touppercase().equals(logincpacha.tostring().touppercase())) { ret.put("type", "error"); ret.put("msg", "验证码错误!"); //实例化日志信息实体 orderinglog orderinglog = new orderinglog("用户名为" user.getouusername() "的用户登录时输入验证码错误!", new date()); //添加日志信息 logservice.addlog(orderinglog); return ret; }
注意:
只有当你把当前系统时间传入该验证码生成工具类中,才能实现点击验证码图片,实时自动替换验证码,如下图所示:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。