---
title: "自己手写一个struts的权限拦截器Interceptor"
author: "dou_shini8820"
date: "2013-04-06T10:40:43Z"
tags: ["Struts", "interceptor", "拦截器"]
categories: ["企业架构"]
slug: "struts-interceptor"
draft: false
lastmod: "2013-04-06T11:09:41Z"
id: 1842592
url: "https://doushini.iteye.com/blog/1842592"
---

1、新建一个类PowerInterceptor实现了Interceptor接口（或者直接继承自AbstractInterceptor）

 

```
package com.luo.action.interceptor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class PowerInterceptor implements Interceptor {

	private static final long serialVersionUID = 1L;
	private String sessionName;
	private String includeName;
	private List<String> nameList;
	
	public void destroy() {
		
	}

	public void init() {
		nameList=getIncludeNameList();
		System.out.println("拦截器初始化："+nameList.size());
	}
	
	public List<String> getIncludeNameList(){
		List<String> list=new ArrayList<String>();
		if(includeName!=null&&!includeName.trim().equals("")){
			String[] names=includeName.trim().split(",");
			for (String name : names) {
				list.add(name);
			}
		}
		return list;
	}

	public String intercept(ActionInvocation invocation) throws Exception {
		String actionname=invocation.getProxy().getActionName();
		if(nameList.contains(actionname)){
			return invocation.invoke();
		}else{
			Map<String,Object> session=invocation.getInvocationContext().getContext().getSession();
			if(session.get(sessionName)==null){
				return "notLogin";
			}else{
				return invocation.invoke();
			}
		}
	}

	//get set
	public void setSessionName(String sessionName) {
		this.sessionName = sessionName;
	}
	public void setIncludeName(String includeName) {
		this.includeName = includeName;
	}
}
```

 

 

权限拦截器主要是不同的url访问时根据是否进行了登录，即是否有session的存在进行拦截。其中有的需要拦截，有的不需要有session也可以访问，比如首页index和跳转到登录页面的action都不需要拦截。

所以这里的变量sessionName是你定义的session中的key ，includeName 是一个以逗号分割的字符串用来存放哪些action是不需要被拦截的。他们的配置在struts.xml文件中：

 

 

```
<package name="ssh" extends="struts-default">
			<interceptors>
				<interceptor name="powerInterceptor" class="com.luo.action.interceptor.PowerInterceptor">
					<param name="sessionName">currentUser</param>
					<param name="includeName">login,userLogin</param>
				</interceptor>
				<interceptor name="mytimer" class="com.luo.action.interceptor.MyTimerInterceptor"></interceptor>
				<interceptor-stack name="mystack">
					<interceptor-ref name="powerInterceptor"/>
					<interceptor-ref name="mytimer"/>
					<interceptor-ref name="defaultStack"/>
				</interceptor-stack>
			</interceptors>
			
			<default-interceptor-ref name="mystack"></default-interceptor-ref>
			
			<global-results>
				<result name="notLogin" type="redirectAction">login.do</result>
			</global-results>
```

  

在interceptors这个节点下面我新配置了自己的powerInterceptor ，他需要有两个参数sessionName ，includeName 。sessionName 在你登录的代码中，登录成功则把当前用户名放到session中，其中的key就是sessionName。

由于要使用自己的拦截器，所以需要新建一个拦截器栈 interceptor-stack，里面用到了默认的defaultStack 和我自己的powerInterceptor  。

为了使拦截器生效，我让我系统默认的拦截器为mystack