1.生成验证码action
这个action主要用来生成验证码图片,首先获取四位随机数,
生成验证码图片后,将其存入session中在登陆action中获取存入session的值和输入的值比较
如果相同则进行账号密码验证,不正确 则返回
package action;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class ImageAction extends ActionSupport {
private ActionContext context = ActionContext.getContext();
public ActionContext getContext() {
return context;
}
public void setContext(ActionContext context) {
this.context = context;
}
HttpServletResponse response = ServletActionContext.getResponse();
public HttpServletResponse getResponse() {
return response;
}
public void setResponse(HttpServletResponse response) {
this.response = response;
}
public String execute() throws Exception{
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
//定义验证码框的大小
int width=60,height=20;
//生成干扰线背景色等
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics g =image.getGraphics();
Random random = new Random();
g.setColor(new Color(122,123,100));
g.fillRect(0, 0, width, height);
g.setFont(new Font("Time New Roman",Font.PLAIN,18));
for(int i=0;i<200;i++){
int x=random.nextInt(width);
int y=random.nextInt(height);
int x1= random.nextInt(12);
int y1=random.nextInt(12);
g.drawLine(x, y, x+x1, y1);
}
String sRand="";
for(int i=0;i<4;i++){
String rand = String.valueOf(random.nextInt(10));
sRand+=rand;
g.setColor(new Color(30+random.nextInt(160),40+random.nextInt(17),40+random.nextInt(180)));
g.drawString(rand, 13*i+6, 16);
}
//根据Random函数自动生成4个数字并将此数字存入session,在登陆验证控制器从session中获取这String与输入的比较
context.getSession().put("yzm", sRand);
g.dispose();
ImageIO.write(image, "JPEG", response.getOutputStream());
//此处应返回null,不然会出现警告
return null;
}
}
2登陆验证action
获取页面输入 的验证码 和存入session的验证码对比是否一样,如果一样则验证用户名密码
不一样则返回登录页面,并将“验证码错误”放入fieldError在页面显示
package action;
import java.io.File;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.validation.SkipValidation;
import bean.UserTable;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import daoIn.IUser;
public class LoginAction extends ActionSupport {
private int id;
private String username;
private String password;
private IUser userdao;
public void setUserdao(IUser userdao) {
this.userdao = userdao;
}
private ActionContext context = ActionContext.getContext();
private UserTable user=new UserTable();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private String uyzm;
public String getUyzm() {
return uyzm;
}
public void setUyzm(String uyzm) {
this.uyzm = uyzm;
}
public String Login() throws Exception {
//从session中获取验证码字符串
String yzm=(String) context.getSession().get("yzm");
//将session中的字符串验证码和输入的比较是否一致,
if(!(uyzm.equals(yzm)))
{
//不一致返回登陆页面并提示警告
this.addFieldError("registerMes", "验证码错误");
return INPUT;
}
user = userdao.checkUser(username.trim(), password.trim());
if (user == null)
{
this.addFieldError("message", getText("user.error"));
return INPUT;
}
else
{
context.getSession().put("user", user);
checkImg(user,context);
context.getSession().put("ADMIN", user.getRank());
return "plateShow()";
}
}
@SkipValidation
public String Youke() throws Exception {
//此处为游客登陆,给游客附默认用户名和密码
//这个方法比较low,自己水平不足想的这个烂方法
user = userdao.checkUser("youke", "youke");
context.getSession().put("user", user);
context.getSession().put("ADMIN", user.getRank());
return "plateShow()";
}
public String Register()throws Exception{
Integer Reg_key=(Integer) context.getSession().get("ADMIN");
if(userdao.check(username.trim())==null){
user.setUsername(username.trim());
user.setPassword(password.trim());
user.setRank(0);
user.setPlateid(0);
userdao.save(user);
if(Reg_key==null){
this.addFieldError("registerMes", "注册成功,请登录");
return LOGIN;
}else
return "userAll()";
}else{
this.addFieldError("registerMes", "用户名已存在");
return "register";
}
}
}
3,登陆页面jsp
<%@ 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>
<title>登录页面</title>
</head>
<body class="gray-bg">
<div class="middle-box text-center loginscreen animated fadeInDown">
<div>
<div>
<h1 class="logo-name">HG</h1>
</div>
<h3>欢迎访问HHD论坛2.0</h3>
<span style="white-space:pre"> </span><s:fielderror cssStyle="color:red;font-size:15px;"/>
<form action="login_Login" Class="m-t" method="post">
<table>
<div class="form-group">
<input type="text" name="username" placeholder="用户名" Class="form-control"/>
</div>
<div class="form-group">
<input type="password" name="password" placeholder="密码" Class="form-control"/>
</div>
<div class="form-group">
<input type="text" name="uyzm" placeholder="验证码" Class="form-control"/>
</div>
<span style="white-space:pre"> </span>//此处生成验证码图片
<div class="form-group">
<img border = 0 src="${pageContext.request.contextPath}/image" >
</div>
<button type="submit" class="btn btn-primary block full-width m-b">登 录</button>
<span style="white-space:pre"> </span> <p class="text-muted text-center"> <small>忘记密码了?</small> | <a href="Register.jsp">注册一个新账号</a>
</p>
<p class="text-muted text-center"><small>不想注册</small></a> | <a href=${pageContext.request.contextPath}/login_Youke>点击游客登录</a>
</p>
<span style="white-space:pre"> </span></table>
</form>
</div>
</div>
</body>
</html>