struts2应用中验证码的生成

1、login.jsp页面程序

  1. <script type="text/javascript">   
  2. function changeValidateCode(obj) {   
  3. //获取当前的时间作为参数,无具体意义   
  4. var timenow = new Date().getTime();   
  5. //每次请求需要一个不同的参数,否则可能会返回同样的验证码   
  6. //这和浏览器的缓存机制有关系,也可以把页面设置为不缓存,这样就不用这个参数了。   
  7. obj.src="rand.action?d="+timenow;   
  8. }   
  9. </script> 
  10.  
  11. 在表单中添加下面这句话:  
  12. <s:text name="random"></s:text><s:textfield name="rand" size="5"></s:textfield><img src="rand.action"  onclick="changeValidateCode(this)" title="点击图片刷新验证码"/> 

2、RandomNumUtil.java 生成验证码的类文件

  1. package org.ml.util;  
  2.  
  3. import java.awt.Color;  
  4. import java.awt.Font;  
  5. import java.awt.Graphics;  
  6. import java.awt.image.BufferedImage;  
  7. import java.io.ByteArrayInputStream;  
  8. import java.io.ByteArrayOutputStream;  
  9. import java.util.Random;  
  10. import javax.imageio.ImageIO;  
  11. import javax.imageio.stream.ImageOutputStream;  
  12.  
  13. public class RandomNumUtil {  
  14.     public static final char[] CHARS = { '2''3''4''5''6''7''8',  
  15.         '9''A''B''C''D''E''F''G''H''J''K''L''M',  
  16.         'N''P''Q''R''S''T''U''V''W''X''Y''Z' };  
  17.     private ByteArrayInputStream image;// 图像  
  18.     private String str;// 验证码  
  19.  
  20.     private RandomNumUtil() {  
  21.         init();// 初始化属性  
  22.     }  
  23.  
  24.     /*  
  25.      * 取得RandomNumUtil实例  
  26.      */ 
  27.     public static RandomNumUtil Instance() {  
  28.         return new RandomNumUtil();  
  29.     }  
  30.  
  31.     /*  
  32.      * 取得验证码图片  
  33.      */ 
  34.     public ByteArrayInputStream getImage() {  
  35.         return this.image;  
  36.     }  
  37.  
  38.     /*  
  39.      * 取得图片的验证码  
  40.      */ 
  41.     public String getString() {  
  42.         return this.str;  
  43.     }  
  44.  
  45.     private void init() {  
  46.         // 在内存中创建图象  
  47.         int width = 85, height = 20;  
  48.         BufferedImage image = new BufferedImage(width, height,  
  49.                 BufferedImage.TYPE_INT_RGB);  
  50.         // 获取图形上下文  
  51.         Graphics g = image.getGraphics();  
  52.         // 生成随机类  
  53.         Random random = new Random();  
  54.         // 设定背景色  
  55.         g.setColor(getRandColor(200250));  
  56.         g.fillRect(00, width, height);  
  57.         // 设定字体  
  58.         g.setFont(new Font("Times New Roman", Font.PLAIN, 18));  
  59.         // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到  
  60.         g.setColor(getRandColor(160200));  
  61.         for (int i = 0; i < 155; i++) {  
  62.             int x = random.nextInt(width);  
  63.             int y = random.nextInt(height);  
  64.             int xl = random.nextInt(12);  
  65.             int yl = random.nextInt(12);  
  66.             g.drawLine(x, y, x + xl, y + yl);  
  67.         }  
  68.         // 取随机产生的认证码(6位数字)  
  69.         StringBuffer sRand = new StringBuffer();    
  70.         for (int i = 0; i < 6; i++) {  
  71.             String rand = String.valueOf(CHARS[random.nextInt(CHARS.length)]);  
  72.             sRand.append(rand);  
  73.                
  74.             // 将认证码显示到图象中  
  75.             g.setColor(new Color(20 + random.nextInt(110), 20 + random  
  76.                     .nextInt(110), 20 + random.nextInt(110)));  
  77.             // 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成  
  78.             g.drawString(rand, 13 * i + 616);  
  79.         }  
  80.         // 赋值验证码  
  81.         this.str = sRand.toString();  
  82.  
  83.         // 图象生效  
  84.         g.dispose();  
  85.         ByteArrayInputStream input = null;  
  86.         ByteArrayOutputStream output = new ByteArrayOutputStream();  
  87.         try {  
  88.             ImageOutputStream imageOut = ImageIO  
  89.                     .createImageOutputStream(output);  
  90.             ImageIO.write(image, "JPEG", imageOut);  
  91.             imageOut.close();  
  92.             input = new ByteArrayInputStream(output.toByteArray());  
  93.         } catch (Exception e) {  
  94.             System.out.println("验证码图片产生出现错误:" + e.toString());  
  95.         }  
  96.  
  97.         this.image = input;/* 赋值图像 */ 
  98.     }  
  99.  
  100.     /*  
  101.      * 给定范围获得随机颜色  
  102.      */ 
  103.     private Color getRandColor(int fc, int bc) {  
  104.         Random random = new Random();  
  105.         if (fc > 255)  
  106.             fc = 255;  
  107.         if (bc > 255)  
  108.             bc = 255;  
  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.     }  

 

3、RandomAction.java 生成验证码的action程序

  1. import java.io.ByteArrayInputStream;  
  2. import org.ml.util.RandomNumUtil;  
  3. import com.opensymphony.xwork2.ActionContext;  
  4. import com.opensymphony.xwork2.ActionSupport;  
  5. public class RandomAction extends ActionSupport{   
  6. private ByteArrayInputStream inputStream;   
  7. public String execute() throws Exception{   
  8. RandomNumUtil rdnu=RandomNumUtil.Instance();   
  9. this.setInputStream(rdnu.getImage());//取得带有随机字符串的图片   
  10. ActionContext.getContext().getSession().put("random", rdnu.getString());//取得随机字符串放入HttpSession   
  11. return SUCCESS;   
  12. }   
  13. public void setInputStream(ByteArrayInputStream inputStream) {   
  14. this.inputStream = inputStream;   
  15. }   
  16. public ByteArrayInputStream getInputStream() {   
  17. return inputStream;   
  18. }  
  19. }  

4、LoginAction.java 验证验证码的action
 

  1. private String rand; //表单中的rand  
  2. public String getRand() {  
  3. return rand;  
  4. }  
  5. public void setRand(String rand) {  
  6. this.rand = rand;  
  7. }  
  8. //从session中取出RandomAction.java 中生成的验证码random  
  9. String arandom=(String)(ActionContext.getContext().getSession().get("random"));  
  10.  
  11. //下面就是将session中保存验证码字符串与客户输入的验证码字符串对比了  
  12. if(arandom.equals(this.getRand())) {  
  13. ActionContext.getContext().getSession().put("user"this.getUsername());  
  14. return SUCCESS;  
  15. }  
  16. else {  
  17. return ERROR;  
  18. }  

5、配置struts.xml文件
 

  1. <!-- Random验证码 --> 
  2. <action name="rand" class="org.ml.rand.RandomAction">     
  3.        <result type="stream">     
  4.             <param name="contentType">image/jpeg</param>     
  5.             <param name="inputName">inputStream</param>     
  6.        </result> 
  7.    </action> 

演示效果

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章