当前位置: 移动技术网 > IT编程>开发语言>Java > java图形验证码生成工具类 web页面校验验证码

java图形验证码生成工具类 web页面校验验证码

2019年07月22日  | 移动技术网IT编程  | 我要评论
最近做验证码,参考网上案例,发现有不少问题,特意进行了修改和完善。 验证码生成器: import javax.imageio.imageio; import

最近做验证码,参考网上案例,发现有不少问题,特意进行了修改和完善。

验证码生成器:

import javax.imageio.imageio; 
import java.awt.*; 
import java.awt.image.bufferedimage; 
import java.io.fileoutputstream; 
import java.io.ioexception; 
import java.io.outputstream; 
import java.util.date; 
import java.util.random; 
 
/** 
 * 验证码生成器 
 * 
 * @author 
 */ 
public class validatecode { 
 // 图片的宽度。 
 private int width = 160; 
 // 图片的高度。 
 private int height = 40; 
 // 验证码字符个数 
 private int codecount = 5; 
 // 验证码干扰线数 
 private int linecount = 150; 
 // 验证码 
 private string code = null; 
 // 验证码图片buffer 
 private bufferedimage buffimg = null; 
 
 // 验证码范围,去掉0(数字)和o(拼音)容易混淆的(小写的1和l也可以去掉,大写不用了) 
 private char[] codesequence = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 
   'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 
   'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; 
 
 /** 
  * 默认构造函数,设置默认参数 
  */ 
 public validatecode() { 
  this.createcode(); 
 } 
 
 /** 
  * @param width 图片宽 
  * @param height 图片高 
  */ 
 public validatecode(int width, int height) { 
  this.width = width; 
  this.height = height; 
  this.createcode(); 
 } 
 
 /** 
  * @param width  图片宽 
  * @param height 图片高 
  * @param codecount 字符个数 
  * @param linecount 干扰线条数 
  */ 
 public validatecode(int width, int height, int codecount, int linecount) { 
  this.width = width; 
  this.height = height; 
  this.codecount = codecount; 
  this.linecount = linecount; 
  this.createcode(); 
 } 
 
 public void createcode() { 
  int x = 0, fontheight = 0, codey = 0; 
  int red = 0, green = 0, blue = 0; 
 
  x = width / (codecount + 2);//每个字符的宽度(左右各空出一个字符) 
  fontheight = height - 2;//字体的高度 
  codey = height - 4; 
 
  // 图像buffer 
  buffimg = new bufferedimage(width, height, bufferedimage.type_int_rgb); 
  graphics2d g = buffimg.creategraphics(); 
  // 生成随机数 
  random random = new random(); 
  // 将图像填充为白色 
  g.setcolor(color.white); 
  g.fillrect(0, 0, width, height); 
  // 创建字体,可以修改为其它的 
  font font = new font("fixedsys", font.plain, fontheight); 
//  font font = new font("times new roman", font.roman_baseline, fontheight); 
  g.setfont(font); 
 
  for (int i = 0; i < linecount; i++) { 
   // 设置随机开始和结束坐标 
   int xs = random.nextint(width);//x坐标开始 
   int ys = random.nextint(height);//y坐标开始 
   int xe = xs + random.nextint(width / 8);//x坐标结束 
   int ye = ys + random.nextint(height / 8);//y坐标结束 
 
   // 产生随机的颜色值,让输出的每个干扰线的颜色值都将不同。 
   red = random.nextint(255); 
   green = random.nextint(255); 
   blue = random.nextint(255); 
   g.setcolor(new color(red, green, blue)); 
   g.drawline(xs, ys, xe, ye); 
  } 
 
  // randomcode记录随机产生的验证码 
  stringbuffer randomcode = new stringbuffer(); 
  // 随机产生codecount个字符的验证码。 
  for (int i = 0; i < codecount; i++) { 
   string strrand = string.valueof(codesequence[random.nextint(codesequence.length)]); 
   // 产生随机的颜色值,让输出的每个字符的颜色值都将不同。 
   red = random.nextint(255); 
   green = random.nextint(255); 
   blue = random.nextint(255); 
   g.setcolor(new color(red, green, blue)); 
   g.drawstring(strrand, (i + 1) * x, codey); 
   // 将产生的四个随机数组合在一起。 
   randomcode.append(strrand); 
  } 
  // 将四位数字的验证码保存到session中。 
  code = randomcode.tostring(); 
 } 
 
 public void write(string path) throws ioexception { 
  outputstream sos = new fileoutputstream(path); 
  this.write(sos); 
 } 
 
 public void write(outputstream sos) throws ioexception { 
  imageio.write(buffimg, "png", sos); 
  sos.close(); 
 } 
 
 public bufferedimage getbuffimg() { 
  return buffimg; 
 } 
 
 public string getcode() { 
  return code; 
 } 
 
 /** 
  * 测试函数,默认生成到d盘 
  * @param args 
  */ 
 public static void main(string[] args) { 
  validatecode vcode = new validatecode(160,40,5,150); 
  try { 
   string path="d:/"+new date().gettime()+".png"; 
   system.out.println(vcode.getcode()+" >"+path); 
   vcode.write(path); 
  } catch (ioexception e) { 
   e.printstacktrace(); 
  } 
 } 
} 

下面是页面js调用验证码

// 刷新图片 
  function changeimg() { 
   var imgsrc = $("#imgobj"); 
   var url = imgsrc.attr("src"); 
   imgsrc.attr("src", changeurl(url)); 
  } 
  //为了使每次生成图片不一致,即不让浏览器读缓存,所以需要加上时间戳 
  function changeurl(url) { 
   var timestamp = (new date()).valueof(); 
   var index = url.indexof("?"); 
   console.log(index); 
   if (index > 0) { 
    url = url.substring(0, url.indexof("?")); 
   } 
   console.log(url); 
   if ((url.indexof("&") > 0)) { 
    url = url + "×tamp=" + timestamp; 
    console.log(url); 
   } else { 
    url = url + "?timestamp=" + timestamp; 
    console.log(url); 
    } 
   return url; 
  } 

下面是controller层输出验证码

/** 
 * 响应验证码页面 
 * @return 
 */ 
@requestmapping(value="/validatecode") 
public string validatecode(httpservletrequest request,httpservletresponse response) throws exception{ 
 // 设置响应的类型格式为图片格式 
 response.setcontenttype("image/jpeg"); 
 //禁止图像缓存。 
 response.setheader("pragma", "no-cache"); 
 response.setheader("cache-control", "no-cache"); 
 response.setdateheader("expires", 0); 
 
 httpsession session = request.getsession(); 
 
 validatecode vcode = new validatecode(120,40,5,100); 
 session.setattribute("code", vcode.getcode()); 
 vcode.write(response.getoutputstream()); 
 return null; 
} 

下面是controller层验证验证码输入是否正确

string code = request.getparameter("code"); 
httpsession session = request.getsession(); 
string sessioncode = (string) session.getattribute("code"); 
if (!stringutils.equalsignorecase(code, sessioncode)) { //忽略验证码大小写 
 throw new runtimeexception("验证码对应不上code=" + code + " sessioncode=" + sessioncode); 
} 


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网