码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC拦截器

时间:2017-05-16 00:41:45      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:bool   用户信息   sso   factor   bat   direct   sep   rri   for   

 

配置springmvc.xml代码:

[java] view plain copy
 
 
 
 技术分享技术分享
  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
 
 
 
 技术分享技术分享
  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.    

SpringMVC拦截器

标签:bool   用户信息   sso   factor   bat   direct   sep   rri   for   

原文地址:http://www.cnblogs.com/mfc-itblog/p/6859039.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!