SSH框架中验证码的集成

1.web.xml

<!--验证码集成-->

<servlet>

<servlet-name>ValidateCodeServlet</servlet-name>

<servlet-class>

com.xjtu.util.ValidateCodeServlet

</servlet-class>

<init-param>

<param-name>width</param-name>

<param-value>200</param-value>

</init-param>

<init-param>

<param-name>height</param-name>

<param-value>80</param-value>

</init-param>

<init-param>

<param-name>codeCount</param-name>

<param-value>5</param-value>

</init-param>

</servlet>

<servlet-mapping>

<servlet-name>ValidateCodeServlet</servlet-name>

<url-pattern>/validateCode.img</url-pattern>

</servlet-mapping>

<!--验证码集成-->

2.com.xjtu.util

packagecom.xjtu.util;

importjava.awt.Color;

importjava.awt.Font;

importjava.awt.Graphics2D;

importjava.awt.image.BufferedImage;

importjava.io.IOException;

importjava.util.Random;

importjavax.imageio.ImageIO;

importjavax.servlet.ServletException;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

publicclassValidateCodeServletextendsHttpServlet{

privatefinalFontmFont=newFont("ArialBlack",Font.PLAIN,15);//设置字体

privatefinalintlineWidth=2;//干扰线的长度=1.414*lineWidth

privatefinalintwidth=60;//定义图形大小

privatefinalintheight=22;//定义图形大小

privatefinalintcount=200;

/**

*描述:

*

*@paramfc

*描述:

*@parambc

*描述:

*

*@return描述:

*/

privateColorgetRandColor(intfc,intbc){//取得给定范围随机颜色

finalRandomrandom=newRandom();

if(fc>255){

fc=255;

}

if(bc>255){

bc=255;

}

finalintr=fc+random.nextInt(bc-fc);

finalintg=fc+random.nextInt(bc-fc);

finalintb=fc+random.nextInt(bc-fc);

returnnewColor(r,g,b);

}

//处理post

@Override

publicvoiddoPost(finalHttpServletRequestrequest,finalHttpServletResponseresponse)

throwsServletException,IOException{

doGet(request,response);

}

/**

*描述:

*

*@paramrequest

*描述:

*@paramresponse

*描述:

*

*@throwsServletException

*描述:

*@throwsIOException

*描述:

*/

@Override

publicvoiddoGet(finalHttpServletRequestrequest,finalHttpServletResponseresponse)

throwsServletException,IOException{

//response.reset();

//设置页面不缓存

response.setHeader("Pragma","No-cache");

response.setHeader("Cache-Control","no-cache");

response.setDateHeader("Expires",0);

response.setContentType("image/gif");

//在内存中创建图象

finalBufferedImageimage=newBufferedImage(width,height,

BufferedImage.TYPE_INT_RGB);

//获取图形上下文

finalGraphics2Dg=(Graphics2D)image.getGraphics();

//生成随机类

finalRandomrandom=newRandom();

//设定背景色

g.setColor(getRandColor(200,250));//---1

g.fillRect(0,0,width,height);

//设定字体

g.setFont(mFont);

//画边框

g.setColor(getRandColor(0,20));//---2

g.drawRect(0,0,width-1,height-1);

//随机产生干扰线,使图象中的认证码不易被其它程序探测到

for(inti=0;i<count;i++){

g.setColor(getRandColor(150,200));//---3

finalintx=random.nextInt(width-lineWidth-1)+1;//保证画在边框之内

finalinty=random.nextInt(height-lineWidth-1)+1;

finalintxl=random.nextInt(lineWidth);

finalintyl=random.nextInt(lineWidth);

g.drawLine(x,y,x+xl,y+yl);

}

//取随机产生的认证码(4位数字)

StringsRand="";

for(inti=0;i<4;i++){

finalStringrand=String.valueOf(random.nextInt(10));

sRand+=rand;

//将认证码显示到图象中,调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成

g.setColor(newColor(20+random.nextInt(130),20+random

.nextInt(130),20+random.nextInt(130)));//--4--50-100

g.drawString(rand,(13*i)+6,16);

}

//将认证码存入SESSION

request.getSession().setAttribute("validateCode",sRand);

//图象生效

g.dispose();

finaljava.io.OutputStreamos=response.getOutputStream();

//输出图象到页面

ImageIO.write(image,"PNG",os);

os.flush();

os.close();

}

}

3.jsp

<inputtype="validate"name="validateCode"id="code"

style="height:18px;width:130px;border:solid1px#cadcb2;font-size:12px;color:#81b432;">

<imgheight='17'width="55"src="${ctx}/validateCode.img"

onclick="this.src='${ctx}/validateCode.img?'+Math.random();"

style="cursor:pointer;vertical-align:middle;"/>

4.form

/*

*GeneratedbyMyEclipseStruts

*Templatepath:templates/java/JavaClass.vtl

*/

packagecom.xjtu.action.form;

importjavax.servlet.http.HttpServletRequest;

importorg.apache.struts.action.ActionErrors;

importorg.apache.struts.action.ActionForm;

importorg.apache.struts.action.ActionMapping;

/**

*MyEclipseStruts

*Creationdate:04-02-2011

*

*XDocletdefinition:

*@struts.formname="loginForm"

*/

publicclassLoginFormextendsActionForm{

/*

*Generatedfields

*/

/**passwordproperty*/

privateStringpassword;

/**usernameproperty*/

privateStringusername;

privateStringvalidateCode;

/*

*GeneratedMethods

*/

/**

*Methodvalidate

*@parammapping

*@paramrequest

*@returnActionErrors

*/

publicActionErrorsvalidate(ActionMappingmapping,

HttpServletRequestrequest){

//TODOAuto-generatedmethodstub

returnnull;

}

/**

*Methodreset

*@parammapping

*@paramrequest

*/

publicvoidreset(ActionMappingmapping,HttpServletRequestrequest){

//TODOAuto-generatedmethodstub

}

/**

*Returnsthepassword.

*@returnString

*/

publicStringgetPassword(){

returnpassword;

}

/**

*Setthepassword.

*@parampasswordThepasswordtoset

*/

publicvoidsetPassword(Stringpassword){

this.password=password;

}

/**

*Returnstheusername.

*@returnString

*/

publicStringgetUsername(){

returnusername;

}

/**

*Settheusername.

*@paramusernameTheusernametoset

*/

publicvoidsetUsername(Stringusername){

this.username=username;

}

publicStringgetValidateCode(){

returnvalidateCode;

}

publicvoidsetValidateCode(StringvalidateCode){

this.validateCode=validateCode;

}

}

5.action

StringvalidateCode=loginForm.getValidateCode();

StringvalidateCodesession=(String)session.getAttribute("validateCode");

if(userinfo.getPwd().equals(password)&&validateCodesession.equals(validateCode))

相关推荐