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. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  18. <property name="dataSource" ref="db2dataSource" />
  19. </bean>
  20. <mvc:interceptors>
  21. <mvc:interceptor>
  22. <!-- 需拦截的地址 -->
  23. <!-- 一级目录 -->
  24. <mvc:mapping path="/*.do" />
  25. <mvc:mapping path="/*.ajax" />
  26. <mvc:mapping path="/*.htm" />
  27. <!-- 二级目录 -->
  28. <mvc:mapping path="/*/*.do" />
  29. <mvc:mapping path="/*/*.ajax" />
  30. <mvc:mapping path="/*/*.htm" />
  31. <!-- 需排除拦截的地址 -->
  32. <mvc:exclude-mapping path="/login.htm"/>
  33. <bean class="com.anxin.msapweb.web.interceptor.SecurityInterceptor" />
  34. </mvc:interceptor>
  35. </mvc:interceptors>
  36. </beans>

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

  1. package com.anxin.msapweb.web.interceptor;
  2. import javax.servlet.http.HttpServletRequest;
  3. import javax.servlet.http.HttpServletResponse;
  4. import javax.servlet.http.HttpSession;
  5. import org.springframework.web.servlet.HandlerInterceptor;
  6. import org.springframework.web.servlet.ModelAndView;
  7. import com.anxin.msapweb.common.Config;
  8. public class SecurityInterceptor implements HandlerInterceptor {
  9. private static final String LOGIN_URL = "/login.htm";
  10. @Override
  11. public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object handler) throws Exception {
  12. HttpSession session = req.getSession(true);
  13. // 从session 里面获取用户名的信息
  14. Object obj = session.getAttribute(Config.Passport.SESSION_NAME_LOGIN_RESULT);
  15. // 判断如果没有取到用户信息,就跳转到登陆页面,提示用户进行登陆
  16. if (obj == null || "".equals(obj.toString())) {
  17. res.sendRedirect(LOGIN_URL);
  18. }
  19. return true;
  20. }
  21. @Override
  22. public void postHandle(HttpServletRequest req, HttpServletResponse res, Object arg2, ModelAndView arg3) throws Exception {
  23. }
  24. @Override
  25. public void afterCompletion(HttpServletRequest req, HttpServletResponse res, Object arg2, Exception arg3) throws Exception {
  26. }
  27. }

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

 

[转]Spring mvc interceptor配置拦截器,没有登录跳到登录页的更多相关文章

  1. Spring mvc interceptor配置拦截器,没有登录跳到登录页

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  2. 1.5(Spring MVC学习笔记) 拦截器(Interceptor)

    一.拦截器 1.1拦截器简介 Spring中的拦截器类似Servlet中的过滤器(Filter),主要用于拦截用户请求, 并进行一定的处理操作(如验证权限.记录日志.设置编码方式等). 1.2拦截器实 ...

  3. Spring Mvc 的自定义拦截器

     spring mvc的拦截器 SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户 ...

  4. Spring MVC中的拦截器/过滤器HandlerInterceptorAdapter的使用

    一般情况下,对来自浏览器的请求的拦截,是利用Filter实现的 而在Spring中,基于Filter这种方式可以实现Bean预处理.后处理. 比如注入FilterRegistrationBean,然后 ...

  5. spring mvc中的拦截器小结 .

    在spring mvc中,拦截器其实比较简单了,下面简单小结并demo下. preHandle:预处理回调方法,实现处理器的预处理(如登录检查),第三个参数为响应的处理器(如我们上一章的Control ...

  6. spring原拦截器配置与新命名空间mvc:interceptors配置拦截器对照与注意事项

    原先,我们是这么配置拦截器的 <bean id="openSessionInViewInterceptor"class="org.springframework.o ...

  7. Spring MVC中自定义拦截器的简单示例

    1. 引言 拦截器(Interceptor)实现对每一个请求处理前后进行相关的业务处理,类似于Servlet的Filter. 我们可以让普通的Bean实现HandlerIntercpetor接口或继承 ...

  8. Spring MVC中的拦截器Interceptor

    谈谈spring中的拦截器 在web开发中,拦截器是经常用到的功能.它可以帮我们验证是否登陆.预先设置数据以及统计方法的执行效率等等.今天就来详细的谈一下spring中的拦截器.spring中拦截器主 ...

  9. Spring MVC 方法注解拦截器

    应用场景,在方法级别对本次调用进行鉴权,如api接口中有个用户唯一标示accessToken,对于有accessToken的每次请求可以在方法加一个拦截器,获得本次请求的用户,存放到request或者 ...

随机推荐

  1. 学习 Linux,302(混合环境): 概念

    http://www.ibm.com/developerworks/cn/linux/l-lpic3-310-1/ 概述 在本文中,学习以下概念: Server Message Block (SMB) ...

  2. Windows平台查看端口占用的程序

    一.方法:管理员权限打开Cmd窗口:netstat -obna

  3. ububuntu配置ip和dns

    装完ubuntu 第一件事情就是连上网,换个源,进行更新操作,但前提条件是要配好ip和dns. 下面把自己配置的过程记录下来,权且当作一份备份,以便不时之需. 一.配置ip ubuntu的网络配置信息 ...

  4. JDK5.0 特性-线程任务执行架构 ScheduledExecutorService

    来自:http://www.cnblogs.com/taven/archive/2011/12/17/2291469.html import java.util.concurrent.Callable ...

  5. 转:清理系统垃圾的BAT代码

    @echo off title @echo off color 2 echo. echo. echo 请不要关闭此窗口! echo. echo 开始清理垃圾文件,请稍等...... echo. ech ...

  6. 分分钟带你玩转 Web Services【1】JAX-WS

    当大型需求被数个公司分割开来,各公司系统相互交换数据的问题就会接踵而来. 毕竟是多家不同的公司的产品,研发开发语言.采用技术框架基本上是百花齐放. 怎样让自家系统提供的服务具有跨平台.跨语言.跨各种防 ...

  7. 【Thinking in Java】读书笔记

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6480258.html 第三章:操作符 1:基本数据类型的比较用 ==.!=,引用类型的==.!=是针对地址的比 ...

  8. java.lang.NoClassDefFoundError: Ljavax/transaction/TransactionManager

    网上下载一个 jta.jar包 放到项目 的WebRoot 的lib文件夹下,解决.

  9. 【Linux】特殊符号$$,$#,$?等解释

    在linux系统中有很多的特殊符号,他们具有特殊的意义 $# 是传给脚本的参数个数 $0 是脚本本身的名字 $1 是传递给该shell的第一个参数 $2 是传递给该shell的第二个参数 $@ 是传递 ...

  10. Echarts柱状图的点击事件

    最近在做一些图表统计的功能,用到了百度的开源图表软件Echatrs,不得不提的是:不但上手简单而且扩展功能也是十分强大.在使用的过程中也遇到了不少问题,可能由于有关Echatrs的资料并不是很齐全,所 ...