`
conkeyn
  • 浏览: 1501253 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

用户登陆,退出等基本Action(1) 验证码

阅读更多

首先需要一个验证码生成的工具类(ConfirmCodeGen.java):

package com.tools;

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

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
 * 生成验证码
 * 
 * @author Michael Young
 * 
 */
public final class ConfirmCodeGen {

	/**
	 * @param response
	 * @param width
	 * @param height
	 * @param len
	 * @return
	 * @throws Exception
	 */
	public static String genImageCode(HttpServletResponse response, int width,
			int height, int len) throws Exception {
		// random number
		String code = Integer.toString((int) (Math.random() * (1E4)));
		for (int i = code.length(); i < len; i++)
			code += "0";
		response.setContentType("image/jpeg");
		response.addHeader("pragma", "NO-cache");
		response.addHeader("Cache-Control", "no-cache");
		response.addDateHeader("Expries", 0);
		BufferedImage image = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		Graphics g = image.getGraphics();
		// 以下填充背景颜色
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, width, height);
		// 画边框
		g.setColor(Color.black);
		g.drawRect(0, 0, width - 1, height - 1);
		// 设置字体颜色
		g.setColor(Color.BLACK);
		// g.setColor(new Color());
		g.setFont(new Font("Times New Roman", Font.BOLD, 16));
		g.drawString(code, 5, 15);
		// 88点
		Random random = new Random();
		for (int i = 0; i < 66; i++) {
			int x = random.nextInt(width);
			int y = random.nextInt(height);
			g.setColor(new Color((int) Math.floor(Math.random() * 255)));
			g.drawLine(x, y, x, y);
		}
		g.dispose();
		ServletOutputStream outStream = response.getOutputStream();
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outStream);
		encoder.encode(image);
		outStream.flush();
		outStream.close();
		return code;
	}

	/**
	 * @param session
	 * @param confirmCode
	 * @return
	 */
	public static boolean isCorrectCode(HttpServletRequest request) {
		HttpSession session = request.getSession();
		String confirmCode = request.getParameter("confirmCode");
		if (session.getAttribute("confirmcode") == null || confirmCode == null) {
			return false;
		}
		String code = (String) session.getAttribute("confirmcode");
		// 移除Session
		session.removeAttribute("confirmcode");
		if (code.equals(confirmCode)) {
			return true;
		}
		return false;
	}

	/**
	 * 从ajax判断验证码的正确与否
	 * 
	 * @param confirmCode
	 *            验证码
	 * @param session
	 * @return
	 */
	public static Byte isCorrectCodeFromSession(String confirmCode,
			HttpSession session) {
		if (session.getAttribute("confirmcode") == null || confirmCode == null) {
			return 0;
		}
		String code = (String) session.getAttribute("confirmcode");
		if (code.equals(confirmCode)) {
			return 1;
		}
		// 移除Session
		session.removeAttribute("confirmcode");
		return 0;

	}
}

  2、需要一个自定义的Action的result,(ConfirmCodeGenResult.java):

/**
 * 
 */
package com.web.struts.results;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.StrutsResultSupport;

import com.opensymphony.xwork2.ActionInvocation;
import com.tools.ConfirmCodeGen;

/**
 * title: 生成的验证码的Result
 * 
 * @author conkeyn
 * @时间 2009-4-14:下午02:31:52
 */
public class ConfirmCodeGenResult extends StrutsResultSupport {

	/**  */
	private static final long serialVersionUID = 2545890371747338210L;

	/** 随机图片宽度 */
	private int c_width;

	/** 随机图片高度 */
	private int c_height;

	/** 随机数长度 */
	private int c_length;

	@Override
	protected void doExecute(String finalLocation, ActionInvocation invocation)
			throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		Object widthO = invocation.getStack().findValue("c_width");
		if (widthO != null) {
			c_width = (Integer) widthO;
			if (c_width <= 0) {
				c_width = 45;
			}
		} else {
			c_width = 45;
		}
		Object heightO = invocation.getStack().findValue("c_height");
		if (heightO != null) {
			c_height = (Integer) heightO;
			if (c_height <= 0) {
				c_height = 20;
			}
		} else {
			c_height = 20;
		}
		Object c_lengthO = invocation.getStack().findValue("c_length");
		if (c_lengthO != null) {
			c_length = (Integer) c_lengthO;
			if (c_length <= 0) {
				c_length = 4;
			}
		} else {
			c_length = 4;
		}
		String random = ConfirmCodeGen.genImageCode(response, c_width,
				c_height, c_length);

		session.setAttribute("confirmcode", random);
	}

	public int getC_width() {
		return c_width;
	}

	public void setC_width(int c_width) {
		this.c_width = c_width;
	}

	public int getC_height() {
		return c_height;
	}

	public void setC_height(int c_height) {
		this.c_height = c_height;
	}

	public int getC_length() {
		return c_length;
	}

	public void setC_length(int c_length) {
		this.c_length = c_length;
	}

}

 3、需要一个默认的action():

package com.web.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 基本Action
 * 
 * 
 * 
 */
public class base extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7546749187705748430L;

	/** 验证码的随机图片宽度 */
	protected int c_width;

	/** 验证码的随机图片高度 */
	protected int c_height;

	/** 验证码的随机数长度 */
	protected int c_length;

	public String execute() throws Exception {
		return SUCCESS;
	}


	/**
	 * Convenience method to get the request
	 * 
	 * @return current request
	 */
	protected HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}


	/**
	 * Convenience method to get the response
	 * 
	 * @return current response
	 */
	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	/**
	 * Convenience method to get the session
	 */
	protected HttpSession getSession() {
		return getRequest().getSession();
	}

	
	public int getC_width() {
		return c_width;
	}

	public void setC_width(int c_width) {
		this.c_width = c_width;
	}

	public int getC_height() {
		return c_height;
	}

	public void setC_height(int c_height) {
		this.c_height = c_height;
	}

	public int getC_length() {
		return c_length;
	}

	public void setC_length(int c_length) {
		this.c_length = c_length;
	}
}

 配置文件(struts-validation.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="map_validation" extends="struts-default" namespace="/validation">
		<result-types>
			<result-type name="confirmCodeGenResult"
				class="com.web.struts.results.ConfirmCodeGenResult" />
		</result-types>
		<action name="confirm" class="baseAction">
			<result type="confirmCodeGenResult"></result>
		</action>
	</package>
</struts>
 

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    action实现验证码

    对登录的验证码进行验证的完整代码,对于新手很方便,易明白,是基于SSH的使用

    struts2生成中文验证码的Action

    struts2生成中文验证码的Action. 非常的简单,也很实用

    struts中实现验证码

    struts中实现验证码,验证码,action

    struts2实现验证码

    主要是用strus2实现验证码功能,里面包含验证码的实现以及xml文件里面的action的书写

    asp生成图片验证码类

    &lt;form method="post" action="from.asp"&gt; &lt;script language=javascript&gt;document.write("&lt;img src=code.asp align=absmiddle id=Image1 onclick=Image1.src='code.asp?'+Math.random(); alt=看不清楚/&gt;"); ...

    生成验证码的项目源码

    生成验证码的源代码。由servlet生成验证码和...能够产生验证码但是如何传输和验证呢,一般是传回服务器在action里做验证。 我还是喜欢js页面产生验证码,因为简单,虽然效果差了点。所以也附带了个js生成验证码的例子。

    javaWeb验证码

    jsp页面显示验证码. JSP输出一张验证码图片..action 验证

    JSP自动生成彩色验证码

    JSP自动生成彩色验证码-由2个JSP页面与一个action验证验证码的类构成,code.jsp自动生成4位彩色随机码。index.jsp是登陆页面包含彩色验证码

    数字验证码(Servlet形式)

    生成验证码,本验证码为Servlet形式,可以凭开发经验,例如在Struts中,将其写在Action的方法中(void类型)从而转为Struts形式,调用时只需使img标签的src属性= 1.(servletname) 2.(methodname_actionname.action)

    STRUTS2验证码实现

    struts2图形验证码实现,两个实现类,和你一个action类

    JAVA-Web课程设计--注册登录系统---用SSH框架整合实现注册登录系.docx

    登录系统的实现 1 1、 系统概述 本次课程设计练习了一个简单的Web系统,该系统实现了用户注册、用户登录两个功能。本系统的实现是基于SSH框架整合技术的。本系统功能单一,业务逻辑简单。主要用于大家练习如何使用...

    struts2 生成验证码

    在struts2下产生验证码,包含所需要的action(返回的就是验证码图片),struts配置文件,页面的调用。

    android 获取短信的验证码

    当系统收到短信时,会发出一个action名称为android.provider.Telephony.SMS_RECEIVED的广播Intent,该Intent存放了接收到的短信内容,使用名称“pdus”即可从Intent中获取短信内容。最好使用动态注册的方法,去注册...

    验证码工具类.zip

    web开发验证码工具类 servlet版和struts2(Action)版两个文件夹,资源包含验证码源码.java文件,.jar包,和使用说明,以及代码样例

    awt制作验证码的源码

    一个在登陆的时候产生验证码的类,可以根据需要转换成自己的action servlet 等 调用的部分可以用一个img 标签关联一个超连接就ok 了

    struts1 用户登录(包含验证)

    使用struts1框架实现用户登录,那么就需要新建两个类,比如:LoginForm、LoginAction继承ActionForm、Action,配置struts-config.xml配置文件

    最全最好用的验证码源码

    这是一个很好的验证码实例!!!带有源码和祥细注释,不看后悔哦!!!&lt;form method="post" action="AdminLogin.jsp" name="f" id="f" check();"&gt; &lt;td height="30"&gt; 验 证 码 &lt;td&gt;&lt;input name="Code...

    一个Action多方法调用的Struts 2的应用程序

    利用Struts 2框架创建一个web项目chap2_e22,实现用户登录过程。具体要求是在loginAction类中分别用login()和registered()处理用户登录和注册的过程,分别创建login.jsp和register.jsp两个页面实现登录和注册的...

    [远程多用户补丁]Win10多用户.rar

    https://dhexx.cn/news/show-318951.html?action=onClick 背景:Win10 正常情况下不允许多用户同时远程,即一个用户远程进来会把另一个用户踢掉,...1.主机修改远程登录相关配置; 2.使用RDPWrap破解远程登录用户限制;

    自动获取短信的验证码

    当系统收到短信时,会发出一个action名称为android.provider.Telephony.SMS_RECEIVED的广播Intent,该Intent存放了接收到的短信内容,使用名称“pdus”即可从Intent中获取短信内容。最好使用动态注册的方法,去注册...

Global site tag (gtag.js) - Google Analytics