[转]Spring mvc interceptor配置拦截器,没有登录跳到登录页

Xml代码  收藏代码
  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>  


注:不支持<mvc:mapping path="*.do" /> 

Java代码  收藏代码
  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. }  

(原文地址:http://qiaolevip.iteye.com/blog/1827676)

 
posted @ 2017-03-16 10:59  HelloSUN  阅读(5559)  评论(0编辑  收藏  举报