登陆验证码(struts2实现)

登陆验证码(struts2实现) 

1.login.jsp 
Java代码   收藏代码
  1. <%@ page language="java" import="java.util.*" pageEncoding="GBK"%>  
  2. <%@ taglib prefix ="s" uri="/struts-tags"%>  
  3. <html>  
  4.   <head>  
  5. <script type="text/javascript">      
  6. function changeValidateCode(obj) {      
  7. /*** 
  8.   *   获取当前的时间作为参数,无具体意义    
  9.   *   每次请求需要一个不同的参数,否则可能会返回同样的验证码     
  10.   *   这和浏览器的缓存机制有关系,也可以把页面设置为不缓存,这样就不用这个参数了。   
  11.   */  
  12. var timenow = new Date().getTime();      
  13.      
  14. obj.src="randPic.action?d="+timenow;      
  15. }      
  16. </script>     
  17. </head>  
  18.     
  19.   <body>  
  20.    <form name="" action="Login">    
  21.   验证码:<s:textfield name="code"></s:textfield><img src="randPic.action"  οnclick="changeValidateCode(this)" title="点击图片刷新验证码"/>   
  22.     <br/><input type="submit" value="登陆"/><input type="reset" value="重置"/>  
  23.   </form>   
  24.   </body>  
  25. </html>  


2.web.xml 
Java代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.4"   
  3.     xmlns="http://java.sun.com/xml/ns/j2ee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
  6.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  7.   <welcome-file-list>  
  8.     <welcome-file>index.jsp</welcome-file>  
  9.   </welcome-file-list>  
  10.   
  11.   <filter>  
  12.     <filter-name>struts2</filter-name>  
  13.     <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  
  14.   </filter>  
  15.   <filter-mapping>  
  16.     <filter-name>struts2</filter-name>  
  17.     <url-pattern>/*</url-pattern>  
  18.   </filter-mapping>  
  19. </web-app>  



3.struts.xml
 
Java代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.   
  6. <struts>  
  7.     <package name="iamge" extends="struts-default" namespace="/">  
  8.         <action name="randPic" class="com.org.momo.action.RandomAction">        
  9.           <result type="stream">        
  10.             <param name="contentType">image/jpeg</param>        
  11.             <param name="inputName">inputStream</param>        
  12.           </result>   
  13.         </action>    
  14.   
  15.         <action name="Login" class="com.org.momo.action.LoginAction">  
  16.             <result name="success">/success.jsp</result>  
  17.             <result name="error">/fail.jsp</result>  
  18.         </action>  
  19.     </package>  
  20. </struts>  


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


5.RandomAction.java和LoginAction.java 
Java代码   收藏代码
  1. package com.org.momo.action ;  
  2.   
  3. import java.io.ByteArrayInputStream;  
  4.   
  5. import com.opensymphony.xwork2.ActionContext;  
  6. import com.opensymphony.xwork2.ActionSupport;  
  7. import com.org.momo.util.RandomNumUtil;  
  8. public class RandomAction extends ActionSupport{      
  9. private ByteArrayInputStream inputStream;      
  10. public String execute() throws Exception{      
  11. RandomNumUtil rdnu=RandomNumUtil.Instance();      
  12. this.setInputStream(rdnu.getImage());//取得带有随机字符串的图片      
  13. ActionContext.getContext().getSession().put("sessionCode", rdnu.getString());//取得随机字符串放入HttpSession      
  14. return SUCCESS;      
  15. }      
  16. public void setInputStream(ByteArrayInputStream inputStream) {      
  17. this.inputStream = inputStream;      
  18. }      
  19. public ByteArrayInputStream getInputStream() {      
  20. return inputStream;      
  21. }     
  22. }   
  23.   
  24.   
  25.   
  26. package com.org.momo.action;  
  27.   
  28. import com.opensymphony.xwork2.ActionContext;  
  29. import com.opensymphony.xwork2.ActionSupport;  
  30.   
  31.   
  32. public class LoginAction extends ActionSupport{  
  33. private String code;     
  34. public String getCode() {     
  35.     return code;     
  36. }     
  37. public void setCode(String code) {     
  38. this.code = code;     
  39. }     
  40.   
  41. public String execute() throws Exception{  
  42.     String sessionCode=(String)(ActionContext.getContext().getSession().get("sessionCode"));      
  43.     if(sessionCode.equals(this.getCode())) {      
  44.         return SUCCESS;     
  45.       }else{     
  46.            return ERROR;     
  47.         }   
  48.    }  
  49. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值