`

登陆验证码(struts2实现)

阅读更多
登陆验证码(struts2实现)

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


2.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <filter>
  	<filter-name>struts2</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>struts2</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>



3.struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <package name="iamge" extends="struts-default" namespace="/">
        <action name="randPic" class="com.org.momo.action.RandomAction">      
          <result type="stream">      
            <param name="contentType">image/jpeg</param>      
            <param name="inputName">inputStream</param>      
          </result> 
        </action>  

        <action name="Login" class="com.org.momo.action.LoginAction">
            <result name="success">/success.jsp</result>
            <result name="error">/fail.jsp</result>
        </action>
    </package>
</struts>


4.RandomNumUtil.java
package com.org.momo.util ;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
public class RandomNumUtil {    
private ByteArrayInputStream image;//图像    
private String str;//验证码      
private RandomNumUtil(){    
	init();//初始化属性    
}    
/*   
* 取得RandomNumUtil实例   
*/    
public static RandomNumUtil Instance(){    
return new RandomNumUtil();    
}    
/*   
* 取得验证码图片   
*/    
public ByteArrayInputStream getImage(){    
return this.image;    
}    
/*   
* 取得图片的验证码   
*/    
public String getString(){    
return this.str;    
}    
  
private void init() {    
// 在内存中创建图象    
int width=85, height=20;    
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);    
// 获取图形上下文    
Graphics g = image.getGraphics();    
// 生成随机类    
Random random = new Random();    
// 设定背景色    
g.setColor(getRandColor(200,250));    
g.fillRect(0, 0, width, height);    
// 设定字体    
g.setFont(new Font("Times New Roman",Font.PLAIN,18));    
// 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到    
g.setColor(getRandColor(160,200));    
for (int i=0;i<155;i++)    
{    
int x = random.nextInt(width);    
int y = random.nextInt(height);    
int xl = random.nextInt(12);    
int yl = random.nextInt(12);    
g.drawLine(x,y,x+xl,y+yl);    
}    
// 取随机产生的认证码(6位数字)    
String sRand="";    
for (int i=0;i<4;i++){    
String rand=String.valueOf(random.nextInt(10));    
sRand+=rand;    
// 将认证码显示到图象中    
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));    
// 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成    
g.drawString(rand,13*i+6,16);    
}   
//赋值验证码   
this.str=sRand;    
  
//图象生效    
g.dispose();    
ByteArrayInputStream input=null;    
ByteArrayOutputStream output = new ByteArrayOutputStream();    
try{    
ImageOutputStream imageOut = ImageIO.createImageOutputStream(output);    
ImageIO.write(image, "JPEG", imageOut);    
imageOut.close();    
input = new ByteArrayInputStream(output.toByteArray());    
}catch(Exception e){    
System.out.println("验证码图片产生出现错误:"+e.toString());    
}    
  
this.image=input;/* 赋值图像 */    
}    
/*   
* 给定范围获得随机颜色   
*/    
private Color getRandColor(int fc,int bc){    
Random random = new Random();    
if(fc>255) fc=255;    
if(bc>255) bc=255;    
int r=fc+random.nextInt(bc-fc);    
int g=fc+random.nextInt(bc-fc);    
int b=fc+random.nextInt(bc-fc);    
return new Color(r,g,b);    
}   
}  


5.RandomAction.java和LoginAction.java
package com.org.momo.action ;

import java.io.ByteArrayInputStream;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.org.momo.util.RandomNumUtil;
public class RandomAction extends ActionSupport{    
private ByteArrayInputStream inputStream;    
public String execute() throws Exception{    
RandomNumUtil rdnu=RandomNumUtil.Instance();    
this.setInputStream(rdnu.getImage());//取得带有随机字符串的图片    
ActionContext.getContext().getSession().put("sessionCode", rdnu.getString());//取得随机字符串放入HttpSession    
return SUCCESS;    
}    
public void setInputStream(ByteArrayInputStream inputStream) {    
this.inputStream = inputStream;    
}    
public ByteArrayInputStream getInputStream() {    
return inputStream;    
}   
} 



package com.org.momo.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


public class LoginAction extends ActionSupport{
private String code;   
public String getCode() {   
	return code;   
}   
public void setCode(String code) {   
this.code = code;   
}   

public String execute() throws Exception{
    String sessionCode=(String)(ActionContext.getContext().getSession().get("sessionCode"));    
    if(sessionCode.equals(this.getCode())) {    
        return SUCCESS;   
      }else{   
           return ERROR;   
        } 
   }
}
 
  • 大小: 27.9 KB
  • 大小: 6.8 KB
分享到:
评论
2 楼 xia635317478 2015-07-28  
jethypc 写道
验证码的session无法传过去啊 还是我的设置有什么问题吗

放session中只是为了点登录之后判断是否正确,struts.xml配置好了就可以传过去了
1 楼 jethypc 2014-06-18  
验证码的session无法传过去啊 还是我的设置有什么问题吗

相关推荐

    struts2实现验证码登陆

    通过struts2实现验证码登录,验证码可以点击更换 ,包含用户名密码校验

    Struts2-登录验证码

    完整的Struts2框架,联系oracle数据库实现登录验证 验证码功能:点击图片切换验证码,验证码错误提示 登录成功后变量session中的list集合 内含备注

    漂亮登陆界面+Struts2 验证码 完整实现

    开发环境: eclipse Kepler JEE IDE + Tomcat v6.0 + Struts-2.3.15(最新版) 步骤: 1. 下载后解压,import to eclipse as a project. 2. 启动 Tomcat v6.0 3. 打开...

    struts2+spring整合登陆验证经典完整案例!

    struts2+spring整合登陆验证经典完整案例!期余JAR文件在struts2+spring整合登陆验证经典完整案例!(JAR)这个中,免积分下载! 部署Web应用请按如下步骤进行: 1. 进入reg_login路径下,将mysql.sql脚本中的语句...

    STRUTS2个人通讯录管理系统

    STRUTS2个人通讯录管理系统实现登陆带验证码分页上传图片mysql数据可

    s2sh带验证码的登陆

    struts2+hibernate3+spring3+mysql+验证码实现登陆

    extjs3.0+struts2源码

    extjs3.0+struts2 实现登陆 验证码 tree树的加载 首页面。 主要代码还是借鉴别人,然后我再原有基础上再加工。 还没添加数据库类。对于初学者来说还是不错的入门源码。

    验证码登陆(SSH初级整合)

    也是用到了SSH整合,主要实现的是验证码登陆,是为了联系SSH整合而专门做的。

    ssh.zip_SSH 实现 上传 下载 功能_java ssh_ssh_struts2.3_上传下载

    struts2.3+hibernate4.1+spring3.1框架整合,采用c3p0连接mysql数据库,实现验证码登陆、curd功能、上传下载等功能。

    struts2.1.8+JPA3.0(hibernate实现)+spring2.5+extjs3.2中型BBS项目

    用户登陆身份验证 随机彩色防识别验证码 内置管理员账户,可以创建、删除、修改用户,并查看用户列表和单个用户详情 普通用户账户,记录用户名和密码在SQL数据库中,支持用户头像上传 普通用户能实现发新帖、...

    myeclipse8.6 SSH+mysql 网页密码登录实例

    myeclipse8.6、JDK1.7、Struts2-Spring-Hibernate,初学SSH 好代码 参考了https://blog.csdn.net/qqq824908000/article/details/76842188,修正了其登录失败会出现的exception 注意要删除低版本的antlr Window--&gt;...

    ssh框架、邮件发送

    ssh框架搭建,采用最新struts2、spring3、hibernate4搭建环境,导入数据库,直接能运行(界面不是很完善),项目中采用velocity模版实现发送邮件功能、实现利用验证码登陆、文件上传下载功能及报表导出功能

    基于Java(SSH)+ionic + MySQL实现电子词典APP【100011513】

    将这些模型实现为基本的 Java 对象,然后编写基本的 DAO 接口,采用 Hibernate 架构实现的 DAO 类来实现 Java 类与数据库之间的转换和访问,最后由 Spring 做支持,支持 struts 和 hibernate。 系统的具体功能 ...

    iuhyiuhkjh908u0980

    做了一个登陆的界面 我想加个验证码 效果如下: 这个验证码的图片是放在 一个textfield里面的 , 我想将验证码放到 这个textfield后面 ,请教 如何实现? login.js 代码如下: LoginPanel = function() { var win, f;...

    JAVA上百实例源码以及开源项目源代码

    第三步:在登陆后的界面文本框输入文本,然后发送 可以同时启动多个客户端 实现群聊。 浮动的广告 嵌套在html中 各种EJB之间的调用示例 7个目标文件 摘要:Java源码,初学实例,EJB调用实例  各种EJB之间的调用源码...

    JAVA上百实例源码以及开源项目

    第三步:在登陆后的界面文本框输入文本,然后发送 可以同时启动多个客户端 实现群聊。 浮动的广告 嵌套在html中 各种EJB之间的调用示例 7个目标文件 摘要:Java源码,初学实例,EJB调用实例  各种EJB之间的调用源码...

    JspRun!社区论坛系统 v6.0 bulid 090423 GBK 源码版.rar

    系统采用struts、hibernate框架及中间件的结合既实现了业务逻辑与控制逻辑的有效分离,提高了层次结构的清晰度,提高了复用的粒度降低了开发代价和维护代价,同时保证了软件的质量使其更具有鲁棒性和可维护性。...

    JspRun!社区论坛系统 v6.0 bulid 090424 GBK 安装版.rar

    系统采用struts、hibernate框架及中间件的结合既实现了业务逻辑与控制逻辑的有效分离,提高了层次结构的清晰度,提高了复用的粒度降低了开发代价和维护代价,同时保证了软件的质量使其更具有鲁棒性和可维护性。...

Global site tag (gtag.js) - Google Analytics