Spring mvc interceptor配置攔截器

[java] view plain copy
 在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" xmlns:context="http://www.springframework.org/schema/context"  
  4.  xmlns:mvc="http://www.springframework.org/schema/mvc"  
  5.  xsi:schemaLocation="http://www.springframework.org/schema/beans    
  6.         http://www.springframework.org/schema/beans/spring-beans.xsd    
  7.         http://www.springframework.org/schema/context    
  8.         http://www.springframework.org/schema/context/spring-context.xsd    
  9.         http://www.springframework.org/schema/mvc    
  10.         http://www.springframework.org/schema/mvc/spring-mvc.xsd"  
  11.  default-autowire="byName">  
  12.  <!-- auto register Processor -->  
  13.  <context:annotation-config />  
  14.  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  15.   <property name="basePackage" value="com.anxin.msapweb.db.mybatis.mapper" />  
  16.  </bean>  
  17.   
  18.  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  19.   <property name="dataSource" ref="db2dataSource" />  
  20.  </bean>  
  21.   
  22.  <mvc:interceptors>  
  23.   <mvc:interceptor>  
  24.    <!-- 需攔截的地址 -->  
  25.    <!-- 一級目錄 -->  
  26.    <mvc:mapping path="/*.do" />  
  27.    <mvc:mapping path="/*.ajax" />  
  28.    <mvc:mapping path="/*.htm" />  
  29.   
  30.    <!-- 二級目錄 -->  
  31.    <mvc:mapping path="/*/*.do" />  
  32.    <mvc:mapping path="/*/*.ajax" />  
  33.    <mvc:mapping path="/*/*.htm" />  
  34.    <!-- 需排除攔截的地址 -->  
  35.    <mvc:exclude-mapping path="/login.htm"/>  
  36.    <bean class="com.anxin.msapweb.web.interceptor.SecurityInterceptor" />  
  37.   </mvc:interceptor>  
  38.  </mvc:interceptors>  
  39. </beans>  


Java代碼:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. package com.anxin.msapweb.web.interceptor;  
  2.   
  3. import javax.servlet.http.HttpServletRequest;  
  4. import javax.servlet.http.HttpServletResponse;  
  5. import javax.servlet.http.HttpSession;  
  6.   
  7. import org.springframework.web.servlet.HandlerInterceptor;  
  8. import org.springframework.web.servlet.ModelAndView;  
  9.   
  10. import com.anxin.msapweb.common.Config;  
  11.   
  12. public class SecurityInterceptor implements HandlerInterceptor {  
  13.   
  14.  private static final String LOGIN_URL = "/login.htm";  
  15.   
  16.  @Override  
  17.  public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object handler) throws Exception {  
  18.   HttpSession session = req.getSession(true);  
  19.   // 從session 裏面獲取用戶名的信息  
  20.   Object obj = session.getAttribute(Config.Passport.SESSION_NAME_LOGIN_RESULT);  
  21.   // 判斷如果沒有取到用戶信息,就跳轉到登陸頁面,提示用戶進行登陸  
  22.   if (obj == null || "".equals(obj.toString())) {  
  23.    res.sendRedirect(LOGIN_URL);  
  24.   }  
  25.   return true;  
  26.  }  
  27.   
  28.  @Override  
  29.  public void postHandle(HttpServletRequest req, HttpServletResponse res, Object arg2, ModelAndView arg3) throws Exception {  
  30.  }  
  31.   
  32.  @Override  
  33.  public void afterCompletion(HttpServletRequest req, HttpServletResponse res, Object arg2, Exception arg3) throws Exception {  
  34.  }  
  35.   
  36. }  
  37.   
  38.    

發佈了50 篇原創文章 · 獲贊 24 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章