Spring拦截器来控制URL路径的转发

Spring拦截器来控制URL路径的转发

 
struts的代码 
  1. import javax.servlet.http.HttpServletRequest;   
  2. import org.aopalliance.intercept.MethodInterceptor;   
  3. import org.aopalliance.intercept.MethodInvocation;   
  4. import org.apache.struts.action.ActionMapping;   
  5.   
  6. /**  
  7.  * 这是一个拦截器,用来验证用户是否通过验证  
  8.  *  
  9.  */  
  10. public class AuthorityInterceptor implements MethodInterceptor {   
  11.   
  12.     public Object invoke(MethodInvocation invocation) throws Throwable   
  13.     {   
  14.         HttpServletRequest request = null;   
  15.         ActionMapping mapping = null;   
  16.         Object[] args = invocation.getArguments();   
  17.         for (int i = 0 ; i < args.length ; i++ )   
  18.         {   
  19.             if (args[i] instanceof HttpServletRequest) request = (HttpServletRequest)args[i];   
  20.             if (args[i] instanceof ActionMapping) mapping = (ActionMapping)args[i];   
  21.         }   
  22.         if ( request.getSession().getAttribute("adminname") != null)   
  23.         {   
  24.             return invocation.proceed();   
  25.         }   
  26.         else  
  27.         {   
  28.             return mapping.findForward("login");   
  29.         }   
  30.     }   
  31. }  
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");
}
}
}


配置文件:
Java代码
  1.    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">   
  2.     <property name="beanNames">   
  3.            <list>   
  4.               <value>/vaiiduser</value>   
  5.                <value>/admin</value>   
  6.               <value>/phone</value>   
  7.            </list>   
  8.     </property>   
  9.        <property name="interceptorNames">   
  10.            <list>   
  11.                <value>authorityInterceptor</value>    
  12.            </list>   
  13.        </property>   
  14.    </bean>   
  15.   
  16. <bean id="authorityInterceptor" class="org.mmc.utils.AuthorityInterceptor"/>

你可能感兴趣的:(Spring拦截器来控制URL路径的转发)