登陆页面的验证码生成与验证
Posted 凌林的芳香
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了登陆页面的验证码生成与验证相关的知识,希望对你有一定的参考价值。
登陆页面的验证码生成与验证
特点:基于strurs2框架 减少对数据库的访问次数 避免无效的访问 提高服务器的性能
1 登陆界面:基于struts2框架 <s:src action>动态获取随机验证码图片
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<form action="${pageContext.request.contextPath}/user/loginUser" method="post">
帐号:<input type="text"user.username"/><br/>
密码:<input type="password" name="user.password"/>
<input name="clientCode" type="text" ><img src="<s:url action="imageAction" namespace="/user" />"/>
<br/>
<input type="submit" value="登录"/><input type="reset" value="取消"/>
</form>
</body>
</html>
2 userAction:
a:getImage()方法中 产生随机4位String验证码 存入值栈session会话中
b:把4为数字转为jpg格式的图片 并把图片传给login页面 并显示出来
c: loginUser()方法中 通过成员变量收参 获取页面输入框中的client输入的验证码 与值栈session中存进的随机产生验证码serviceCode进行比较 , 相同则验证通过 允许访问数据库 不相同则返回登陆页面 并提示“验证码错误”
package com.baizhi.nick.action;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.baizhi.nick.entity.Users;
import com.baizhi.nick.service.UserServiceImpl;
import com.baizhi.nick.validimage.SecurityCode;
import com.baizhi.nick.validimage.SecurityImage;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;
public class UserAction extends ActionSupport {
private Users user;
private String clientCode;
//登陆
public String loginUser(){
System.out.println("---- im loginUser---- "+user);
//值栈
ValueStack vs=ActionContext.getContext().getValueStack();
//get serviceCode
String serviceCode = (String) vs.findValue("#session.code");
if(!serviceCode.equals(clientCode)){
vs.setValue("#request.errMsg", "验证码不正确");
return "errorlogin";
}
//invoking service
UserServiceImpl service=new UserServiceImpl();
user=service.loginUser(user);
if(user==null){
vs.setValue("#request.errMsg", "改用户未注册,请注册!");
return "errorlogin";
}
//set user
vs.setValue("#session.user", user);
System.out.println("查询结果:"+user);
return "queryAll";
}
//注册
public String registUser(){
UserServiceImpl service=new UserServiceImpl();
if(user.getUsername()==null){
return "errorregist";
}
service.registUser(user);
return "login";
}
//验证码
public String getImage() throws IOException{
//1 获取4为验证码
String serviceCode = SecurityCode.getSecurityCode();
//2 把验证码--》session
ActionContext.getContext().getValueStack().setValue("#session.code", serviceCode);
//3 根据验证码 生成图片
BufferedImage bufferedImage = SecurityImage.createImage(serviceCode);
//4 把图片写到client
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("image/jpeg");
ServletOutputStream out = response.getOutputStream();
ImageIO.write(bufferedImage, "jpg", out);
return null;
}
public Users getUser() {
return user;
}
public void setUser(Users user) {
this.user = user;
}
public String getClientCode() {
return clientCode;
}
public void setClientCode(String clientCode) {
this.clientCode = clientCode;
}
}
3:配置文件xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd" >
<struts>
<package name="users" extends="basepage" namespace="/user">
<action name="loginUser" class="com.baizhi.nick.action.UserAction" method="loginUser">
<result name="errorlogin">/jsp/login.jsp</result>
<result name="queryAll" type="redirectAction">
<param name="namespace">/person</param>
<param name="actionName">queryAll</param>
</result>
</action>
<!-- 验证码 -->
<action name="imageAction" class="com.baizhi.nick.action.UserAction" method="getImage">
</action>
</package>
<include file="com/baizhi/nick/struts/struts-person.xml"></include>
</struts>
以上是关于登陆页面的验证码生成与验证的主要内容,如果未能解决你的问题,请参考以下文章