基于SpringMVC的登录拦截器

  1.Struts2的核心过滤器配置

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1.  <!-- Struts2的核心过滤器配置 -->  
  2.     <filter>  
  3.         <filter-name>struts2</filter-name>  
  4.         <filter-  
  5.   
  6. class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter  
  7.   
  8. -class>  
  9.     </filter>  
  10.     <!-- Struts2过滤器拦截所有的.action请求 -->  
  11.     <filter-mapping>  
  12.         <filter-name>struts2</filter-name>  
  13.         <url-pattern>*.action</url-pattern>  
  14.     </filter-mapping>  
    

    2.springmvc-servlet.xml配置

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.   
  5. xmlns:p="http://www.springframework.org/schema/p"  
  6.     xmlns:beans="http://www.springframework.org/schema/beans"  
  7.     xmlns:context="http://www.springframework.org/schema/context"  
  8.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  9.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  10.             http://www.springframework.org/schema/beans/spring-beans.xsd  
  11.             http://www.springframework.org/schema/context   
  12.             http://www.springframework.org/schema/context/spring-context.xsd  
  13.             http://www.springframework.org/schema/aop   
  14.             http://www.springframework.org/schema/aop/spring-aop.xsd  
  15.             http://www.springframework.org/schema/tx   
  16.             http://www.springframework.org/schema/tx/spring-tx.xsd  
  17.             http://www.springframework.org/schema/mvc   
  18.             http://www.springframework.org/schema/mvc/spring-mvc.xsd">  
  19.   
  20.     <!-- 启用spring mvc 注解(默认的注解映射的支持) -->  
  21.     <mvc:annotation-driven />  
  22.           
  23.     <!-- 设置使用注解的类所在的jar包(自动扫描的包名) -->  
  24.     <context:component-scan  
  25.         base-package="com.ouc.ulab.controller" />  
  26.   
  27.     <!--进行静态资源的访问 -->  
  28.     <mvc:resources mapping="/static/**" location="/static/" />  
  29.   
  30.     <!-- 配置资源文件,防止被拦截 -->  
  31.     <!-- <mvc:resources location="/WEB-INF/view/image/" mapping="/image/**"/>   
  32.         <mvc:resources location="/WEB-INF/view/js/" mapping="/js/**"/>   
  33.   
  34. <mvc:resources   
  35.         location="/WEB-INF/view/css/" mapping="/css/**"/> -->  
  36.   
  37.     <!-- 拦截器 -->  
  38.     <mvc:interceptors>  
  39.        <bean   
  40.   
  41. class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">  
  42.             <property name="paramName" value="lang" ></property>  
  43.        </bean>  
  44.     </mvc:interceptors>   
  45.       
  46.     <mvc:interceptors>  
  47.         <mvc:interceptor>  
  48.             <mvc:mapping path="/*.do" />  
  49.             <mvc:mapping path="/*.ajax" />  
  50.             <mvc:mapping path="/*.jsp" />  
  51.             <mvc:mapping path="/*.html" />  
  52.   
  53.             <!-- 二级目录 -->  
  54.             <mvc:mapping path="/*/*.do" />  
  55.             <mvc:mapping path="/*/*.ajax" />  
  56.             <mvc:mapping path="/*/*.jsp" />  
  57.             <mvc:mapping path="/*/*.html" />  
  58.   
  59.             <!-- 需排除拦截的地址 -->  
  60.             <mvc:exclude-mapping path="/login.jsp" />  
  61.             <mvc:exclude-mapping path="/login.do" />  
  62.             <mvc:exclude-mapping path="/getUserLoginInfo.do" />  
  63.              
  64.             <bean   
  65.   
  66. class="com.ouc.ulab.interceptor.UserSecurityInterceptor"></bean>  
  67.         </mvc:interceptor>  
  68.     </mvc:interceptors>  
  69.   
  70.     <mvc:interceptors>  
  71.       <mvc:interceptor>  
  72.            <mvc:mapping path="/*.do" />  
  73.             <mvc:mapping path="/*.ajax" />  
  74.             <mvc:mapping path="/*.jsp" />  
  75.             <mvc:mapping path="/*.html" />  
  76.   
  77.             <mvc:mapping path="/*/*.do" />  
  78.             <mvc:mapping path="/*/*.ajax" />  
  79.             <mvc:mapping path="/*/*.jsp" />  
  80.             <mvc:mapping path="/*/*.html" />   
  81.               
  82.             <mvc:exclude-mapping path="/login.jsp" />  
  83.             <mvc:exclude-mapping path="/login.do" />  
  84.             <mvc:exclude-mapping path="/loadHome.do" />  
  85.                         <mvc:exclude-mapping path="/getUserLoginInfo.do" />  
  86.               
  87.             <bean   
  88.   
  89. class="com.ouc.ulab.interceptor.AuthoritySecurityInterceptor" >   
  90.             </bean>  
  91.         </mvc:interceptor>  
  92.     </mvc:interceptors>   
  93.       
  94.     <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 对模型视图名称的解析,即  
  95.   
  96. 在模型视图名称添加前后缀 InternalResourceViewResolver默认的就是JstlView所以这里就不  
  97.   
  98. 用配置viewClass了 -->  
  99.     <bean   
  100.   
  101. class="org.springframework.web.servlet.view.InternalResourceViewResolver"  
  102.         p:prefix="/" p:suffix=".jsp">  
  103.     </bean>  
  104.   
  105.     <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射, 配置一个基于注解  
  106.   
  107. 的定制的WebBindingInitializer,解决日期转换问题,方法级别的处理器映射 -->  
  108.     <bean   
  109.   
  110. class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapte  
  111.   
  112. r">  
  113.         <!-- 配置一下对json数据的转换 -->  
  114.         <property name="messageConverters">  
  115.             <list>  
  116.                 <bean   
  117.   
  118. class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">  
  119.   
  120. </bean>  
  121.             </list>  
  122.         </property>  
  123.     </bean>  
  124. </beans>  


     3. 登陆拦截器 UserSecurityInterceptor

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. package com.ouc.ulab.interceptor;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Enumeration;  
  5. import java.util.List;  
  6.   
  7. import java.net.URLEncoder;  
  8.   
  9. import javax.servlet.http.HttpServletRequest;  
  10. import javax.servlet.http.HttpServletResponse;  
  11. import javax.servlet.http.HttpSession;  
  12.   
  13. import org.apache.commons.lang.StringUtils;  
  14. import org.springframework.web.servlet.HandlerInterceptor;  
  15. import org.springframework.web.servlet.ModelAndView;  
  16. import org.springframework.web.method.HandlerMethod;  
  17.   
  18. import com.haier.openplatform.security.LoginContext;  
  19. import com.haier.openplatform.security.LoginContextHolder;  
  20. import com.haier.openplatform.security.SessionSecurityConstants;  
  21.   
  22. import com.ouc.ulab.security.BaseUser;  
  23.   
  24. public class UserSecurityInterceptor implements HandlerInterceptor{  
  25.   
  26.     //private List<String> excludedUrls;  
  27.       
  28.     private static final String DEFAULT_LOGIN = "/login.jsp";    
  29.       
  30.     //public final static String SESSIONNAME = "sessionName";  
  31.       
  32.     private List<String> noLoginAuthUrlList = new ArrayList<String>();  
  33.     /** 
  34.      * 用户session中的用户表示 
  35.      */  
  36.     private String keyUserName = SessionSecurityConstants.KEY_USER_NAME;  
  37.     private String keyUserNickName =   
  38.   
  39. SessionSecurityConstants.KEY_USER_NICK_NAME;  
  40.     private String keyUserId = SessionSecurityConstants.KEY_USER_ID;  
  41.     private String keyLocalLanguage =   
  42.   
  43. SessionSecurityConstants.KEY_LOCAL_LANGUAGE;  
  44.   
  45.     public void setKeyUserName(String keyUserName) {  
  46.         this.keyUserName = keyUserName;  
  47.     }  
  48.   
  49.     public void setKeyUserId(String keyUserId) {  
  50.         this.keyUserId = keyUserId;  
  51.     }  
  52.   
  53.     public void setKeyLocalLanguage(String keyLocalLanguage) {  
  54.         this.keyLocalLanguage = keyLocalLanguage;  
  55.     }  
  56.   
  57.     public void setNoLoginAuthUrlList(List<String> noLoginAuthUrlList) {  
  58.         this.noLoginAuthUrlList = noLoginAuthUrlList;  
  59.     }  
  60.       
  61.     /*public List<String> getExcludedUrls() { 
  62.        return excludedUrls; 
  63.     } 
  64.  
  65.     public void setExcludedUrls(List<String> excludedUrls) { 
  66.         this.excludedUrls = excludedUrls; 
  67.     }*/  
  68.   
  69.     /**  
  70.      * preHandle方法是进行处理器拦截用的,顾名思义,该方法将在Controller处理之前进 
  71.  
  72. 行调用,SpringMVC中的Interceptor拦截器是链式的,可以同时存在  
  73.      * 多个Interceptor,然后SpringMVC会根据声明的前后顺序一个接一个的执行,而且所有 
  74.  
  75. 的Interceptor中的preHandle方法都会在  
  76.      * Controller方法调用之前调用。SpringMVC的这种Interceptor链式结构也是可以进行中 
  77.  
  78. 断的,这种中断方式是令preHandle的返  
  79.      * 回值为false,当preHandle的返回值为false的时候整个请求就结束了。  
  80.      */    
  81.     @SuppressWarnings("deprecation")  
  82.     @Override  
  83.     public boolean preHandle(HttpServletRequest req, HttpServletResponse res,  
  84.             Object obj) throws Exception {  
  85.         System.out.println("-------在Action之前执行,如果返回true,则继续向后  
  86.   
  87. 执行--------");  
  88.           
  89.         /*//请求的路径 
  90.         String contextPath = req.getContextPath(); 
  91.         String url= req.getServletPath().toString(); 
  92.         System.out.println(contextPath); 
  93.         HttpSession session = req.getSession(); 
  94.          
  95.         BaseUser user = new BaseUser(); 
  96.         user.setUserId((Long)session.getAttribute(keyUserId)); 
  97.         user.setUserName((String)session.getAttribute(keyUserName)); 
  98.         user.setNickName((String)session.getAttribute(keyUserNickName)); 
  99.          
  100.         System.out.println("用户名:"+user.getUserName()); 
  101.          
  102.         if(!isLogin(req)){ 
  103.             res.sendRedirect(contextPath + "/login.jsp?redirectURL=" 
  104.                     + URLEncoder.encode(url)); 
  105.             return false; 
  106.         } 
  107.          
  108.         return true;*/  
  109.           
  110.               
  111.         //请求的路径  
  112.         String contextPath = req.getContextPath();  
  113.         String  url= req.getServletPath().toString();  
  114.         System.out.println(contextPath);  
  115.         HttpSession session = req.getSession();  
  116.         String userName = (String) session.getAttribute(keyUserName);  
  117.         System.out.println("用户名:"+userName);  
  118.         //这里可以根据session的用户来判断角色的权限,根据权限来重定向不同的页面,简  
  119.   
  120. 单起见,这里只是做了一个重定向  
  121.         if (StringUtils.isEmpty(userName)) {  
  122.             //被拦截,重定向到login界面  
  123.             res.sendRedirect(contextPath + DEFAULT_LOGIN + "?redirectURL="  
  124.                     + URLEncoder.encode(url));  
  125.             return false;  
  126.         }  
  127.         return true;  
  128.           
  129.     }  
  130.       
  131.     /**  
  132.      * 该方法也是需要当前对应的Interceptor的preHandle方法的返回值为true时才会执行。 
  133.  
  134. 该方法将在整个请求完成之后,也就是DispatcherServlet渲染了视图执行,  
  135.      * 这个方法的主要作用是用于清理资源的,当然这个方法也只能在当前这个Interceptor 
  136.  
  137. 的preHandle方法的返回值为true时才会执行。  
  138.      */   
  139.       
  140.     @Override  
  141.     public void afterCompletion(HttpServletRequest arg0,  
  142.             HttpServletResponse arg1, Object arg2, Exception ex)  
  143.             throws Exception {  
  144.         // TODO Auto-generated method stub  
  145.         System.out.println("----在Action 方法执行完毕之后,无论是否抛出异常,通常用来  
  146.   
  147. 进行异常处理----------");  
  148.     }  
  149.       
  150.     /**  
  151.      * 这个方法只会在当前这个Interceptor的preHandle方法返回值为true的时候才会执行。 
  152.  
  153. postHandle是进行处理器拦截用的,它的执行时间是在处理器进行处理之  
  154.      * 后,也就是在Controller的方法调用之后执行,但是它会在DispatcherServlet进行视 
  155.  
  156. 图的渲染之前执行,也就是说在这个方法中你可以对ModelAndView进行操  
  157.      * 作。这个方法的链式结构跟正常访问的方向是相反的,也就是说先声明的Interceptor 
  158.  
  159. 拦截器该方法反而会后调用,这跟Struts2里面的拦截器的执行过程有点像,  
  160.      * 只是Struts2里面的intercept方法中要手动的调用ActionInvocation的invoke方法, 
  161.  
  162. Struts2中调用ActionInvocation的invoke方法就是调用下一个Interceptor  
  163.      * 或者是调用action,然后要在Interceptor之前调用的内容都写在调用invoke之前,要 
  164.  
  165. 在Interceptor之后调用的内容都写在调用invoke方法之后。  
  166.      */    
  167.     @Override  
  168.     public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,  
  169.             Object arg2, ModelAndView arg3) throws Exception {  
  170.         // TODO Auto-generated method stub  
  171.          System.out.println("----在Action方法执行完毕之后,执行(没有抛异常的  
  172.   
  173. 话)----------");  
  174.     }  
  175.       
  176.     protected boolean isLogin(HttpServletRequest httpServletRequest) {  
  177.         String currentUrl = httpServletRequest.getRequestURI();  
  178.           
  179.         noLoginAuthUrlList.add("/login.jsp");  
  180.           
  181.         for(String url : noLoginAuthUrlList){  
  182.             if(currentUrl.startsWith(url)){  
  183.                 return true;  
  184.             }  
  185.         }  
  186.           
  187.         HttpSession httpSession = httpServletRequest.getSession();  
  188.         String userName = (String) httpSession.getAttribute(keyUserName);  
  189.         if (userName == null) {  
  190.             // 仅仅记住get请求的链接  
  191.             if (StringUtils.equalsIgnoreCase  
  192.   
  193. (httpServletRequest.getMethod(),  
  194.                     "GET")) {  
  195.                 HttpSession session =   
  196.   
  197. httpServletRequest.getSession();  
  198.                 String servletPath =   
  199.   
  200. httpServletRequest.getServletPath();  
  201.                 String fullURL = new StringBuffer  
  202.   
  203. (servletPath).append(  
  204.                         toParameterString  
  205.   
  206. (httpServletRequest)).toString();  
  207.                 session.setAttribute(  
  208.                           
  209.   
  210. SessionSecurityConstants.KEY_LAST_VISIT_URL, fullURL);  
  211.             }  
  212.             return false;  
  213.         }  
  214.         return true;  
  215.     }  
  216.       
  217.     /** 
  218.      *  
  219.      * @param httpServletRequest 
  220.      * @return 
  221.      */  
  222.     private String toParameterString(HttpServletRequest httpServletRequest) {  
  223.         Enumeration<String> paramEnumeration =   
  224.   
  225. httpServletRequest.getParameterNames();  
  226.         if (!paramEnumeration.hasMoreElements()) {  
  227.             return "";  
  228.         }  
  229.         StringBuffer stringBuffer = new StringBuffer();  
  230.         while (paramEnumeration.hasMoreElements()) {  
  231.             String paramName = paramEnumeration.nextElement();  
  232.             stringBuffer.append("&");  
  233.             stringBuffer.append(paramName);  
  234.             stringBuffer.append("=");  
  235.             stringBuffer.append(httpServletRequest.getParameter  
  236.   
  237. (paramName));  
  238.         }  
  239.         stringBuffer.replace(01"?");  
  240.         return stringBuffer.toString();  
  241.     }  
  242. }  

    4. 权限拦截器:AuthoritySecurityInterceptor

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. package com.ouc.ulab.interceptor;  
  2.   
  3. import java.util.List;  
  4.   
  5. import javax.servlet.http.HttpServletRequest;  
  6. import javax.servlet.http.HttpServletResponse;  
  7. import javax.servlet.http.HttpSession;  
  8.   
  9. import org.apache.commons.lang.StringUtils;  
  10. import org.springframework.beans.factory.annotation.Autowired;  
  11. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  12. import org.springframework.web.servlet.HandlerInterceptor;  
  13. import org.springframework.web.servlet.ModelAndView;  
  14.   
  15. import com.haier.openplatform.hac.dto.HacResourceDTO;  
  16. import com.haier.openplatform.hac.resource.dto.enu.ResourceTypeEnum;  
  17. import com.haier.openplatform.hac.resource.service.HacResourceServiceClient;  
  18. import com.haier.openplatform.security.AbstractAuthenticator;  
  19. import com.haier.openplatform.security.Authentication;  
  20. import com.haier.openplatform.security.DefaultUrlAuthenticator;  
  21. import com.haier.openplatform.security.SessionSecurityConstants;  
  22. import com.ouc.ulab.interceptor.AuthResources;  
  23.   
  24. public class AuthoritySecurityInterceptor implements HandlerInterceptor {  
  25.       
  26.     @Autowired  
  27.     private static HacResourceServiceClient resourceServiceClient;   
  28.       
  29.     /** 
  30.      * 无权限跳转的页面 
  31.      */  
  32.     private static final String NOAUTHPAGE = "/NoAuth.jsp";  
  33.   
  34.     //当前用户所拥有的资源  
  35.     private List<HacResourceDTO> resources;  
  36.       
  37.     //判断资源是否已获取的布尔变量  
  38.     private boolean HACRES_ISGET = false;  
  39.       
  40.     /**  
  41.      * preHandle方法是进行处理器拦截用的,顾名思义,该方法将在Controller处理之前进 
  42.  
  43. 行调用,SpringMVC中的Interceptor拦截器是链式的,可以同时存在  
  44.      * 多个Interceptor,然后SpringMVC会根据声明的前后顺序一个接一个的执行,而且所有 
  45.  
  46. 的Interceptor中的preHandle方法都会在  
  47.      * Controller方法调用之前调用。SpringMVC的这种Interceptor链式结构也是可以进行中 
  48.  
  49. 断的,这种中断方式是令preHandle的返  
  50.      * 回值为false,当preHandle的返回值为false的时候整个请求就结束了。  
  51.      */   
  52.       
  53.     @Override  
  54.     public boolean preHandle(HttpServletRequest request,  
  55.             HttpServletResponse response, Object handler) throws   
  56.   
  57. Exception {  
  58.         HttpSession httpSession = request.getSession();  
  59.           
  60.         // 根据应用编码、用户名、当前语言和版本获取该用户所拥有的权限列表  
  61.           (此处调用外部dubbo接口获取当前登录用户的资源列表)  
  62.           
  63.         if (!HACRES_ISGET){  
  64.             ClassPathXmlApplicationContext cxt = new   
  65.   
  66. ClassPathXmlApplicationContext(  
  67.                     new String[]{"classpath:dubbo-  
  68.   
  69. consumer.xml","classpath:springmvc-servlet.xml"},true);  
  70.               
  71.             cxt.start();  
  72.               
  73.             resourceServiceClient = (HacResourceServiceClient)  
  74.   
  75. cxt.getBean("resourceServiceClient");  
  76.               
  77.             resources = resourceServiceClient.getResourcesByAppAndUser  
  78.   
  79. ("H001""wangxi""zh_CN""1.0");  
  80.               
  81.             HACRES_ISGET = true;  
  82.         }  
  83.           
  84.         System.out.println("王玺所拥有的权限列表:" + resources.size());  
  85.         for(int i=0;i<resources.size();i++)  
  86.         {  
  87.             System.out.println("资源序号: " + (i+1));  
  88.             System.out.println("AppName: " + resources.get  
  89.   
  90. (i).getAppName());  
  91.             System.out.println("AppCode: " + resources.get  
  92.   
  93. (i).getAppCode());  
  94.             System.out.println("BaseUrl: " + resources.get  
  95.   
  96. (i).getBaseUrl());  
  97.             System.out.println("UserId: " + resources.get(i).getUserId  
  98.   
  99. ());  
  100.             System.out.println("AppId: " + resources.get(i).getAppId  
  101.   
  102. ());  
  103.             System.out.println("CreateBy: " + resources.get  
  104.   
  105. (i).getCreateBy());  
  106.             System.out.println("Code: " + resources.get(i).getCode());  
  107.             System.out.println("Configuration: " + resources.get  
  108.   
  109. (i).getConfiguration());  
  110.             System.out.println("Name: " + resources.get(i).getName());  
  111.             System.out.println("ModuleId: " + resources.get  
  112.   
  113. (i).getModuleId());  
  114.             System.out.println("Url: " + resources.get(i).getUrl());  
  115.             System.out.println("FileId: " + resources.get(i).getFileId  
  116.   
  117. ());  
  118.             System.out.println("Status: " + resources.get(i).getStatus  
  119.   
  120. ());  
  121.             System.out.println();  
  122.         }  
  123.           
  124.         // KEY_AUTHENTICATION  用户授权信息  
  125.         String authentication = (String)httpSession.getAttribute  
  126.   
  127. (SessionSecurityConstants.KEY_AUTHENTICATION);  
  128.           
  129.         AuthResources auth = new AuthResources();  
  130.           
  131.         for(HacResourceDTO resource : resources){  
  132.             ResourceTypeEnum resourceType = ResourceTypeEnum.toEnum  
  133.   
  134. (resource.getType());  
  135.             if(resourceType == ResourceTypeEnum.URL_RESOURCE ||   
  136.   
  137. resourceType == ResourceTypeEnum.TODO_RESOURCE){  
  138.                 auth.getUrlResources().add(resource.getUrl());  
  139.                 if(StringUtils.isNotEmpty(resource.getCode())){  
  140.                     auth.getComponentResources().add  
  141.   
  142. (resource.getCode());  
  143.                 }  
  144.             }else if(resourceType ==   
  145.   
  146. ResourceTypeEnum.COMPONENT_RESOURCE){  
  147.                 auth.getComponentResources().add(resource.getCode  
  148.   
  149. ());  
  150.             }  
  151.         }  
  152.           
  153.         if (authentication == null || auth.getUrlResources().isEmpty()) {  
  154.             // Session存放的用戶登录名   KEY_USER_NAME  
  155.             String userCode = (String)httpSession.getAttribute  
  156.   
  157. (SessionSecurityConstants.KEY_USER_NAME);  
  158.             //authentication = getAuthentication(userCode, request);  
  159.             //httpSession.setAttribute  
  160.   
  161. (SessionSecurityConstants.KEY_AUTHENTICATION, authentication);  
  162.         }  
  163.           
  164.         String contextPath = request.getContextPath();  
  165.         String currentUrl = request.getServletPath();  
  166.         System.out.println("目前的URL:"+currentUrl);  
  167.           
  168.         if(auth.getUrlResources().size()>0){  
  169.             for(int j=0; j<auth.getUrlResources().size();j++){  
  170.                 System.out.println(auth.getUrlResources().get(j));  
  171.                 if(auth.getUrlResources().get(j).equals  
  172.   
  173. (currentUrl)){  
  174.                     return true;  
  175.                 }  
  176.             }  
  177.         }  
  178.               
  179.         /*if(!authenticator.hasUrlAuth(currentUrl)){ 
  180.             response.sendRedirect(NOAUTHPAGE); 
  181.             return false; 
  182.         }*/  
  183.           
  184.         response.sendRedirect(contextPath + NOAUTHPAGE);  
  185.   
  186.         return false;  
  187.     }  
  188.       
  189.     /**  
  190.      * 该方法也是需要当前对应的Interceptor的preHandle方法的返回值为true时才会执行。 
  191.  
  192. 该方法将在整个请求完成之后,也就是DispatcherServlet渲染了视图执行,  
  193.      * 这个方法的主要作用是用于清理资源的,当然这个方法也只能在当前这个Interceptor 
  194.  
  195. 的preHandle方法的返回值为true时才会执行。  
  196.      */   
  197.       
  198.     @Override  
  199.     public void afterCompletion(HttpServletRequest arg0,  
  200.             HttpServletResponse arg1, Object arg2, Exception ex)  
  201.             throws Exception {  
  202.         // TODO Auto-generated method stub  
  203.         System.out.println("----在Action 方法执行完毕之后,无论是否抛出异常,通常用来  
  204.   
  205. 进行异常处理----------");  
  206.     }  
  207.       
  208.     /**  
  209.      * 这个方法只会在当前这个Interceptor的preHandle方法返回值为true的时候才会执行。 
  210.  
  211. postHandle是进行处理器拦截用的,它的执行时间是在处理器进行处理之  
  212.      * 后,也就是在Controller的方法调用之后执行,但是它会在DispatcherServlet进行视 
  213.  
  214. 图的渲染之前执行,也就是说在这个方法中你可以对ModelAndView进行操  
  215.      * 作。这个方法的链式结构跟正常访问的方向是相反的,也就是说先声明的Interceptor 
  216.  
  217. 拦截器该方法反而会后调用,这跟Struts2里面的拦截器的执行过程有点像,  
  218.      * 只是Struts2里面的intercept方法中要手动的调用ActionInvocation的invoke方法, 
  219.  
  220. Struts2中调用ActionInvocation的invoke方法就是调用下一个Interceptor  
  221.      * 或者是调用action,然后要在Interceptor之前调用的内容都写在调用invoke之前,要 
  222.  
  223. 在Interceptor之后调用的内容都写在调用invoke方法之后。  
  224.      */    
  225.     @Override  
  226.     public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,  
  227.             Object arg2, ModelAndView arg3) throws Exception {  
  228.         // TODO Auto-generated method stub  
  229.          System.out.println("----在Action方法执行完毕之后,执行(没有抛异常的  
  230.   
  231. 话)----------");  
  232.     }  
  233.       
  234. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值