`

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

阅读更多
<?xml  version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans.xsd  
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context.xsd  
        http://www.springframework.org/schema/mvc   
        http://www.springframework.org/schema/mvc/spring-mvc.xsd"
	default-autowire="byName">
	<!-- auto register Processor -->
	<context:annotation-config />
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.anxin.msapweb.db.mybatis.mapper" />
	</bean>

	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="db2dataSource" />
	</bean>

	<mvc:interceptors>
		<mvc:interceptor>
			<!-- 需拦截的地址 -->
                        <!-- 一级目录 -->
			<mvc:mapping path="/*.do" />
			<mvc:mapping path="/*.ajax" />
			<mvc:mapping path="/*.htm" />

                        <!-- 二级目录 -->
			<mvc:mapping path="/*/*.do" />
			<mvc:mapping path="/*/*.ajax" />
			<mvc:mapping path="/*/*.htm" />
			<!-- 需排除拦截的地址 -->
			<mvc:exclude-mapping path="/login.htm"/>
			<bean class="com.anxin.msapweb.web.interceptor.SecurityInterceptor" />
		</mvc:interceptor>
	</mvc:interceptors>
</beans>


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

package com.anxin.msapweb.web.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.anxin.msapweb.common.Config;

public class SecurityInterceptor implements HandlerInterceptor {

	private static final String LOGIN_URL = "/login.htm";

	@Override
	public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object handler) throws Exception {
		HttpSession session = req.getSession(true);
		// 从session 里面获取用户名的信息
		Object obj = session.getAttribute(Config.Passport.SESSION_NAME_LOGIN_RESULT);
		// 判断如果没有取到用户信息,就跳转到登陆页面,提示用户进行登陆
		if (obj == null || "".equals(obj.toString())) {
			res.sendRedirect(LOGIN_URL);
		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest req, HttpServletResponse res, Object arg2, ModelAndView arg3) throws Exception {
	}

	@Override
	public void afterCompletion(HttpServletRequest req, HttpServletResponse res, Object arg2, Exception arg3) throws Exception {
	}

}
分享到:
评论
6 楼 SummerOnline 2017-07-10  
5 楼 aitouchy 2015-12-15  
把http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd改成http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
4 楼 lt26i 2015-05-03  
是不是因为这个没有排除啊

spring-mvc.xml

   <property name="excludeUrls">
     <list>
标签没有把对应的访问地址放入value
3 楼 QMonkey 2014-05-08  
caishenluli 写道
<mvc:exclude-mapping>并没有排除掉,
<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/to/**"/>
        <mvc:exclude-mapping path="/to/idx"/>
        <bean class="com.share.UserLoginInterceptor"/>
    </mvc:interceptor>
</mvc:interceptors>



应该是你先配置了/to/**,就会拦截所有/to下面的请求,就包括了/to/idx,所以下面的排出不起作用了。
2 楼 caishenluli 2013-10-05  
<mvc:exclude-mapping>并没有排除掉,
<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/to/**"/>
        <mvc:exclude-mapping path="/to/idx"/>
        <bean class="com.share.UserLoginInterceptor"/>
    </mvc:interceptor>
</mvc:interceptors>
1 楼 caishenluli 2013-10-05  
楼主,<mvc:exclude-mapping> 不起作用 ,你知道是怎么回事不?

相关推荐

    基于java的企业级应用开发:拦截器.ppt

    15.1 拦截器概述 Spring MVC中的拦截器(Interceptor)类似于Servlet中的过滤器(Filter),它主要用于拦截用户请求并作相应的处理。例如通过拦截器可以进行权限验证、记录请求信息的日志、判断用户是否登录等。 ...

    Spring Cloud OpenFeign - - - >拦截器

    初学者很容易将 Spring MVC 拦截器 和 Spring Cloud OpenFeign 拦截器搞混,误以为OpenFeign拦截器就是SpringMVC拦截器:Spring MVC拦截器发生在客户端 和 服务端之间,在客户端向服务端发送请求时进行拦截处理。...

    Spring MVC 拦截器 interceptor 用法详解

    主要介绍了Spring MVC 拦截器 interceptor 用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

    Mybatis拦截器记录数据更新历史记录到MongoDB

    Mybatis拦截器记录数据更新历史记录到MongoDB的源码,另外需要配置拦截器到mybatis配置文件中。

    springmvc-interceptor

    springmvc中关于拦截器的使用

    拦截器和控制器的区别

    4、拦截器可以利用依赖注入,因此在spring框架程序中,优先拦截器 5、拦截器是包裹在过滤器中使用的。 复习 converter 转换器 i18n struts2 spring MVC 拦截器 interceptor 过滤器 filter web.xml ...

    SpringBoot 自定义拦截器 的 源代码

    我们对拦截器并不陌生,无论是 Struts 2 还是 Spring MVC 中都提供了拦截器功能,它可以根据 URL 对请求进行拦截,主要应用于登陆校验、权限验证、乱码解决、性能监控和异常处理等功能上。Spring Boot 同样提供了...

    在spring-boot工程中添加spring mvc拦截器

    主要介绍了在spring-boot工程中添加spring mvc拦截器,Spring MVC的拦截器(Interceptor)不是Filter,同样可以实现请求的预处理、后处理。,需要的朋友可以参考下

    Spring MVC拦截器的基本使用方法

    主要给大家介绍了关于Spring MVC拦截器的基本使用方法,文中通过示例代码介绍的非常详细,对大家学习或者使用Spring MVC具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧

    java后台权限管理系统源码(spring boot+mvc+mybatis).zip

    项目基于jdk1.8整合了springboot+mvc+mybatis(通用mapper)+druid+jsp+bootstrap等技术,springboot+Listener(监听器),Filter(过滤器),Interceptor(拦截器),Servlet,springmvc静态资源,文件上传下载,多数据源切换,缓存...

    详解SpringMVC中使用Interceptor拦截器

    SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理,这篇文章主要介绍了详解SpringMVC中使用Interceptor拦截器,有兴趣的可以了解一下。

    spring boot+mvc+mybatis(通用mapper)+druid+jsp+bootstrap实现后台权限管理系统源码

    项目基于jdk1.8整合了springboot+mvc+mybatis(通用mapper)+druid+jsp+bootstrap等技术,springboot+Listener(监听器),Filter(过滤器),Interceptor(拦截器),Servlet,springmvc静态资源,文件上传下载,多数据源切换,缓存...

    spring-method-interceptor:Spring Handler Interceptor反思Web方法

    弹簧方法拦截器 该示例显示了一个HandlerInterceptor来拦截Web调用并注销注释值。 此示例使用启动2,唯一的区别是您将使用WebMvcConfigurer注册拦截器。

    springboot权限控制系统

    项目基于jdk1.8整合了springboot+mvc+mybatis(通用mapper)+druid+jsp+bootstrap等技术,springboot+Listener(监听器),Filter(过滤器),Interceptor(拦截器),Servlet,springmvc静态资源,文件上传下载,多数据源切换,缓存...

    整合springboot+mvc+mybatis(通用mapper)+druid+jsp+bootstrap实现权限管理文件上传下载多数据源切换等功能

    项目基于jdk1.8整合了springboot+mvc+mybatis(通用mapper)+druid+jsp+bootstrap等技术,springboot+Listener(监听器),Filter(过滤器),Interceptor(拦截器),Servlet,springmvc静态资源,文件上传下载,多数据源切换,缓存...

    权限管理系统 shiro + ssm实现

    项目基于jdk1.8整合了springboot+mvc+mybatis(通用mapper)+druid+jsp+bootstrap等技术,springboot+Listener(监听器),Filter(过滤器),Interceptor(拦截器),Servlet,springmvc静态资源,文件上传下载,多数据源切换,缓存...

    springboot+权限管理系统 shiro + ssm实现 实现菜单,自用

    项目基于jdk1.8整合了springboot+mvc+mybatis(通用mapper)+druid+jsp+bootstrap等技术,springboot+Listener(监听器),Filter(过滤器),Interceptor(拦截器),Servlet,springmvc静态资源,文件上传下载,多数据源切换,缓存...

    springmybatis

    mybatis实战教程mybatis in action之六与Spring MVC 的集成 mybatis实战教程mybatis in action之七实现mybatis分页源码下载 mybatis实战教程mybatis in action之八mybatis 动态sql语句 mybatis实战教程mybatis in ...

    Java 权限管理系统 shiro + ssm实现

    项目基于jdk1.8整合了springboot+mvc+mybatis(通用mapper)+druid+jsp+bootstrap等技术,springboot+Listener(监听器),Filter(过滤器),Interceptor(拦截器),Servlet,springmvc静态资源,文件上传下载,多数据源切换,缓存...

    springboot+mybatis+druid+jsp+bootstrap实现后台权限管理系统源码.zip

    项目基于jdk1.8整合了springboot+mvc+mybatis(通用mapper)+druid+jsp+bootstrap等技术,springboot+Listener(监听器),Filter(过滤器),Interceptor(拦截器),Servlet,springmvc静态资源,文件上传下载,多数据源切换,缓存...

Global site tag (gtag.js) - Google Analytics