struts的代碼
- import javax.servlet.http.HttpServletRequest;
- import org.aopalliance.intercept.MethodInterceptor;
- import org.aopalliance.intercept.MethodInvocation;
- import org.apache.struts.action.ActionMapping;
- /**
- * 這是一個攔截器,用來驗證用戶是否通過驗證
- *
- */
- public class AuthorityInterceptor implements MethodInterceptor {
- public Object invoke(MethodInvocation invocation) throws Throwable
- {
- HttpServletRequest request = null;
- ActionMapping mapping = null;
- Object[] args = invocation.getArguments();
- for (int i = 0 ; i < args.length ; i++ )
- {
- if (args[i] instanceof HttpServletRequest) request = (HttpServletRequest)args[i];
- if (args[i] instanceof ActionMapping) mapping = (ActionMapping)args[i];
- }
- if ( request.getSession().getAttribute("adminname") != null)
- {
- return invocation.proceed();
- }
- else
- {
- return mapping.findForward("login");
- }
- }
- }
配置文件:
- <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
- <property name="beanNames">
- <list>
- <value>/vaiiduser</value>
- <value>/admin</value>
- <value>/phone</value>
- </list>
- </property>
- <property name="interceptorNames">
- <list>
- <value>authorityInterceptor</value>
- </list>
- </property>
- </bean>
- <bean id="authorityInterceptor" class="org.mmc.utils.AuthorityInterceptor"/>