我用struts2做的登录验证,刚开始自定义了个结果集用来输出验证码,可是在写了一个验证是否登录的自定义拦截器以后,验证码不能输出了,找了两天了都没找到原因。下面是主要代码,请各位前辈帮忙指点一下
//这是自定义的拦截器,用来判断是否登录
package xyz.mrya.interceptor;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import xyz.mrya.domain.Student;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
public class LoginInterceptor extends MethodFilterInterceptor {
private static final long serialVersionUID = 1L;
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
HttpSession session = ServletActionContext.getRequest().getSession();
Student student = (Student) session.getAttribute("student");
if (student != null) {
String msg = invocation.invoke();
System.out.println("已登录-"+msg);
return msg;
}
else{
System.out.println("未登录");
return Action.LOGIN;
}
}
}
//自定义的结果类型,用来输出验证码
package xyz.mrya.result;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.Result;
public class CheckCodeResult implements Result {
private static final long serialVersionUID = 1L;
@Override
public void execute(ActionInvocation arg0) throws Exception {
System.out.println("验证码");
HttpServletResponse response = ServletActionContext.getResponse();
HttpServletRequest request = ServletActionContext.getRequest();
String codeStr = makeCode();
request.getSession().setAttribute("checkCode", codeStr);
BufferedImage image = draw2DCode(codeStr, 70, 25);
ImageIO.write(image, "gif", response.getOutputStream());
}
public BufferedImage draw2DCode(String codeString, int width, int height) {
Random r = new Random();
int x = 10;
int y = 20;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = (Graphics2D) image.getGraphics();
g2d.setColor(new Color(r.nextInt(36) + 219, r.nextInt(36) + 219, r
.nextInt(36) + 219));
g2d.fillRect(0, 0, width, height);
drawSpot(g2d, width, height);
for (int i = 0; i < codeString.length(); i++) {
double ratian = Math.toRadians(30 - r.nextInt(60));
g2d.setColor(new Color(r.nextInt(188), r.nextInt(188), r
.nextInt(188)));
int fontSize = r.nextInt(6);
// int isBold = Math.random() < 0.5 ? Font.BOLD : Font.PLAIN;
g2d.setFont(new Font("宋体", Font.BOLD, (20 + fontSize)));
g2d.rotate(ratian, x + i * 13, y);
g2d.drawString(codeString.toCharArray()[i] + "", x + i * 13, y);
g2d.rotate(-ratian, x + i * 13, y);
}
return image;
}
public void drawSpot(Graphics2D g2d, int width, int height) {
Random r = new Random();
int spotNum = r.nextInt(10) + 60;
for (int i = 0; i < spotNum; i++) {
Color color = new Color(r.nextInt(255), r.nextInt(255),
r.nextInt(255));
g2d.setColor(color);
g2d.fillOval(r.nextInt(width), r.nextInt(height), r.nextInt(5),
r.nextInt(5));
}
}
// 产生验证码
public String makeCode() {
Random r = new Random();
String s = "";
for (int i = 0; i < 4; i++) {
int n = r.nextInt(3);
switch (n) {
case 0:
s = s + String.valueOf(r.nextInt(9));
break;
case 1:
s = s + (char) (r.nextInt(26) + 97);
break;
case 2:
s = s + (char) (r.nextInt(26) + 65);
break;
}
}
return s;
}
}
//一个登录的action
package xyz.mrya.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import xyz.mrya.domain.Student;
import xyz.mrya.service.BusinessService;
import xyz.mrya.service.impl.BusinessServiceImpl;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String studentId;
private String password;
private String checkCode;
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getCheckCode() {
return checkCode;
}
public void setCheckCode(String checkCode) {
this.checkCode = checkCode;
}
public String login() {
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
String reqCheckCode = (String) session.getAttribute("checkCode");
System.out.println("LoginAction");
if (reqCheckCode == null||checkCode==null) {
return ERROR;
}
if(!reqCheckCode.toUpperCase().equals(checkCode.toUpperCase())){
return ERROR;
}
BusinessService bs = new BusinessServiceImpl();
Student student = bs.login(studentId, password);
if (student != null) {
session.setAttribute("student", student);
return SUCCESS;
}
return ERROR;
}
}
<?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>
<constant name="struts.devMode" value="true"></constant>
<constant name="struts.action.entension" value="action,,"></constant>
<constant name="struts.ui.theme" value="simple"></constant>
<package name="power" extends="struts-default">
<result-types >
<result-type name="checkCode" class="xyz.mrya.result.CheckCodeResult"></result-type>
</result-types>
<!--..................................................................................... -->
<interceptors>
<interceptor name="checklogin" class="xyz.mrya.interceptor.LoginInterceptor">
<param name="excludeMethods">login</param>
</interceptor>
<interceptor-stack name="myDefaultStack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="checklogin"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!--注解掉下面这句,验证码就能显示了 -->
<default-interceptor-ref name="myDefaultStack" />
<!--..................................................................................... -->
<global-results>
<result name="login">/WEB-INF/student/login.jsp</result>
</global-results>
<!--..................................................................................... -->
<action name="login" class="xyz.mrya.action.LoginAction"
method="login">
<result>/WEB-INF/student/main.jsp</result>
<result name="error">/WEB-INF/student/login.jsp</result>
</action>
<!--find只为测试是否登录,-->
<action name="find">
<result>/WEB-INF/student/main.jsp</result>
</action>
<action name="checkCode">
<result name="success" type="checkCode"></result>
</action>
<!--..................................................................................... -->
</package>
</struts>
在注释掉拦截器后,验证码才能正常显示,各位大神帮忙
------解决思路----------------------
那是因为你的登录拦截器把验证码也拦截,你修改下拦截器,让验证码通过