Struts2中实现随机验证码

一.创建RandomNum类

  1: import java.awt.Color;
  2: import java.awt.Font;
  3: import java.awt.Graphics;
  4: import java.awt.image.BufferedImage;
  5: import java.io.ByteArrayInputStream;
  6: import java.io.ByteArrayOutputStream;
  7: import java.io.IOException;
  8: import java.util.Random;
  9: import javax.imageio.ImageIO;
 10: import javax.imageio.stream.ImageOutputStream;
 11: 
 12: public class RandomNumUtil {
 13:  private ByteArrayInputStream image; //字节流输出图像
 14:  private String str; //随机数组成的字符串
 15: 
 16:  public void setImage(ByteArrayInputStream image) {
 17:   this.image = image;
 18:  }
 19: 
 20:  public void setStr(String str) {
 21:   this.str = str;
 22:  }
 23:  
 24:  private RandomNumUtil(){
 25:   init();//初始化属性
 26:  }
 27:  /*
 28:   * 取得RandomNumUtil实例
 29:   */
 30:  public static RandomNumUtil Instance() {
 31:   return new RandomNumUtil();
 32:  }
 33:  /*
 34:   * 取得验证码图片
 35:   */
 36:  public ByteArrayInputStream getImage() {
 37:   return image;
 38:  }
 39:  /*
 40:   * 取得图片的验证码
 41:   */
 42:  public String getStr() {
 43:   return str;
 44:  }
 45:  /*
 46:   * init()方法,为字节流赋值
 47:   */
 48:  private void init(){
 49:   //在内存中创建图像
 50:   int width=85,height=20;
 51:   BufferedImage image=new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
 52:   //获取图形上下文
 53:   Graphics g=image.getGraphics();
 54:   //生成随机类
 55:   Random random=new Random();
 56:   //设定背景色
 57:   g.setColor(getRandColor(200,250));
 58:   g.fillRect(0, 0, width, height);
 59:   //设定字体
 60:   g.setFont(new Font("Times New Roman",Font.PLAIN,18));
 61:   //随机产生155条干扰线,使图像中的认证码不易被其它程序探测到
 62:   g.setColor(getRandColor(160, 200));
 63:   for (int i = 0; i < 155; i++) {
 64:    int x=random.nextInt(width);
 65:    int y=random.nextInt(height);
 66:    int x1=random.nextInt(12);
 67:    int y1=random.nextInt(12);
 68:    g.drawLine(x, y, x+x1, y+y1);
 69:   }
 70:   //取随机产生的认证码(6位数)
 71:   String sRand="";
 72:   for (int i = 0; i < 6; i++) {
 73:    String rand=String.valueOf(random.nextInt(10));
 74:    sRand+=rand;
 75:    //将认证码显示到图像中
 76:    g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
 77:    //
 78:    g.drawString(rand, 13*i+6, 16);
 79:   }
 80:    //赋值验证码
 81:    this.str=sRand;
 82:    //图像生效
 83:    g.dispose();
 84:    ByteArrayInputStream input=null;
 85:    ByteArrayOutputStream output=new ByteArrayOutputStream();
 86:    try {
 87:     ImageOutputStream imageOut=ImageIO.createImageOutputStream(output);
 88:     ImageIO.write(image, "JPEG", imageOut);
 89:     imageOut.close();
 90:     input=new ByteArrayInputStream(output.toByteArray());
 91:    } catch (IOException e) {
 92:     System.out.println("验证码图片产生出现错误:"+e.toString());
 93:     e.printStackTrace();
 94:    }
 95:    this.image=input;
 96:  }
 97:  
 98:  /*
 99:   * 给定范围获得随机颜色
100:   */
101:  private Color getRandColor(int fc,int bc) {
102:   Random random = new Random();
103:   if(fc>255){
104:    fc=255;
105:   }
106:   if(bc>255){
107:    bc=255;
108:   }
109:   int r=fc+random.nextInt(bc-fc);
110:   int g=fc+random.nextInt(bc-fc);
111:   int b=fc+random.nextInt(bc-fc);
112:   return new Color(r, g, b);
113:  }
114: }
二.创建RandomAction
获取图像流,并放到sessio中
  1: package com.action;
  2: 
  3: import java.io.ByteArrayInputStream;
  4: 
  5: import com.opensymphony.xwork2.ActionContext;
  6: import com.opensymphony.xwork2.ActionSupport;
  7: import com.util.RandomNumUtil;
  8: 
  9: public class RandomAction extends ActionSupport{
 10:  private ByteArrayInputStream inputStream;
 11: 
 12:  public ByteArrayInputStream getInputStream() {
 13:   return inputStream;
 14:  }
 15: 
 16:  public void setInputStream(ByteArrayInputStream inputStream) {
 17:   this.inputStream = inputStream;
 18:  }
 19:  
 20:  public String execute() throws Exception{
 21:   
 22:   RandomNumUtil rand=RandomNumUtil.Instance();
 23:   this.setInputStream(rand.getImage());
 24:   ActionContext.getContext().getSession().put("random", rand.getStr());// 取得随机字符串放入HttpSession
 25:   
 26:   return "success";
 27:  }
 28: }
 29: 
三.配置Struts.xml
  1: <package name="random" extends="struts-default">
  2: 		<!-- Random验证码 -->
  3: 		<action name="rand" class="com.action.RandomAction">
  4: 			<result type="stream"> <!-- 以流类型返回结果 -->
  5: 				<param name="contentType">image/jpeg</param>
  6: 				<param name="inputName">inputStream</param>
  7: 			</result>
  8: 		</action>
  9: 	</package>
四.在登录页面添加
  1: <s:textfield label="验证码"  name="rand" size="6" />
  2:       <image src="rand.action"
  3:         onclick="changeValidateCode(this)" title="点击图片刷新验证码" />
五.在loginAction中添加验证码验证
  1: String random = (String) ActionContext.getContext().getSession().get("random");
  2:   if (random.equals(rand)) {
  3: 
  4:   } else {
  5: 
  6:   }
posted @ 2013-12-05 14:17  iLincoln  阅读(278)  评论(0编辑  收藏  举报