---
title: "利用spring拦截struts(1.x)中的action"
author: "maybe723"
date: "2008-10-26T18:14:07Z"
tags: []
slug: "spring-struts-1-x-action"
draft: false
lastmod: "2008-10-26T18:14:07Z"
id: 258211
url: "https://maybe723.iteye.com/blog/258211"
---

在struts-config.xml中配置:

```
 <controller
  processorClass="org.springframework.web.struts.DelegatingRequestProcessor">
 </controller>
```

 新建AuthMethodInteceptor:

```
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.struts.action.ActionMapping;

import entity.Users;

public class AuthMethodInterceptor implements MethodInterceptor {

	public Object invoke(MethodInvocation invocation) throws Throwable {
		Object[] object=invocation.getArguments();
		try
		{
			HttpServletRequest request = (HttpServletRequest) invocation.getArguments()[2];
			ActionMapping mapping=(ActionMapping) invocation.getArguments()[0];
			Users user = (Users) (request.getSession().getAttribute("user"));
			
			if(user==null)
			{
			     return mapping.findForward("login");
		
			}
			Object returnObject = invocation.proceed();
		                return returnObject;

		}
		catch(Exception e)
		{
			
		}
		
		return object;
	}
}
```

 

   spring配置文件中:

 

 

```
<bean id="authInterceptor" class="utils.AuthMethodInterceptor">

	</bean>


	<bean
		class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
		<property name="interceptorNames">
			<list>
				<value>authInterceptor</value>
			</list>
		</property>
		<property name="beanNames">
			<list>
				<value>/*insert</value>

				
			</list>
		</property>

	</bean>
```