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> 

演示效果

 

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