目录
自己编写的interceptor 需要继承MethodFilterInterceptor父类:
创建的action类需要在登录时将登录信息传入session中供拦截器识别:
在普通的action登录验证中虽然能验证密码 却可以直接访问登录后的界面 用拦截器后可以拦截请求 使访问到别的页面的用户跳转到登录页面进行登录
测试用的login页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/login.action">
用户名:<input type="text" name="username"><br>
密码<input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
自己编写的interceptor 需要继承MethodFilterInterceptor父类:
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
import com.xin.domain.User;
import org.apache.struts2.ServletActionContext;
public class CheckLoginInterceptor extends MethodFilterInterceptor {
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
System.out.println("校验登录的拦截器....");
// TODO Auto-generated method stub
//1.获取登录成功传入的sesson的user
User user = (User) ServletActionContext.getRequest().getSess