spring拦截器(interceptor)简介
1. 拦截器用途
(1)拦截未登录用户直接访问某些链接
(2)拦截日志信息
(3)拦截非法攻击,比如sql注入
2. 涉及jar、类
(1)spring-webmvc.jar
(2)HandlerInterceptor(org.springframework.web.servlet:接口)、
AsyncHandlerInterceptor(org.springframework.web.servlet:接口)、
HandlerInterceptorAdapter(org.springframework.web.servlet.handler.HandlerInterceptorAdapter:抽象类)

3.业务类
(1)实现(implements) 实现HandlerInterceptor接口或者子接口
(2)继承(extends) 继承HandlerInterceptor接口子类(抽象类)
(3)涉及的方法
preHandle、postHandle、afterCompletion
4.测试代码
LoginInterceptor
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler throws Exception {
LOGGER.info("--------preHandle-------" + request.getRequestURI());
HttpSession session = request.getSession();
String login_account = String.valueOf(session.getAttribute(CommonConstants.SESSION_KEY+session.getId()));
if(!request.getRequestURI().contains("/baselogin/")){
if (StringUtils.isBlank(login_account) || "null".equalsIgnoreCase(login_account)) {
response.sendRedirect("/baselogin/loginPage.htm");
return false;
9 }
}
return true;
}
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)throws Exception {
LOGGER.info("--------postHandle-------" + request.getRequestURI());
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
LOGGER.info("--------afterCompletion-------" + request.getRequestURI());
}
public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
23 LOGGER.info("--------afterConcurrentHandlingStarted-------" + request.getRequestURI());
}
执行顺序:preHandle -> controller -> postHandle -> afterCompletion
配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
">
<!-- 拦截排除
<mvc:interceptors>
<mvc:interceptor>
<mvc:exclude-mapping path=""/>
<bean class="org.bighead.interceptor.LoginInterceptor" />
</mvc:interceptor>
</mvc:interceptors> -->
<!-- 拦截登录 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/*/*.htm"/>
<bean class="org.bighead.interceptor.LoginInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
<beans>
spring拦截器(interceptor)简介的更多相关文章
- web 过滤器 Filter、 Spring 拦截器 interceptor
1.过滤器(Filter)(在web.xml中注册过滤器) 首先说一下Filter的使用地方,我们在配置web.xml时,总会配置下面一段设置字符编码,不然会导致乱码问题: <filter> ...
- spring拦截器Interceptor
在Spring Boot中,拦截器可以分为两种类型: 一是WebMVC,负责拦截请求,类似于过滤器,对用户的请求在Controller接收前进行处理,在Controller处理完成后加工结果等.使用时 ...
- spring 拦截器简介
spring 拦截器简介 常见应用场景 1.日志记录:记录请求信息的日志,以便进行信息监控.信息统计.计算PV(Page View)等.2.权限检查:如登录检测,进入处理器检测检测是否登录,如果没有直 ...
- Spring中过滤器(Filter)和拦截器(Interceptor)的区别和联系
在我们日常的开发中,我们经常会用到Filter和Interceptor.有时同一个功能.Filter可以做,Interceptor也可以做.有时就需要考虑使用哪一个比较好.这篇文章主要介绍一下,二者的 ...
- 5-21 拦截器 Interceptor
Spring MVC拦截器 什么是拦截器 拦截器是SpringMvc框架提供的功能 它可以在控制器方法运行之前或运行之后(还有其它特殊时机)对请求进行处理或加工的特定接口 常见面试题:过滤器和拦截器的 ...
- Spring拦截器中通过request获取到该请求对应Controller中的method对象
背景:项目使用Spring 3.1.0.RELEASE,从dao到Controller层全部是基于注解配置.我的需求是想在自定义的Spring拦截器中通过request获取到该请求对应于Control ...
- java之拦截器Interceptor
1,拦截器的概念 java里的拦截器是动态拦截Action调用的对象,它提供了一种机制可以使开发者在一个Action执行的前后执行一段代码,也可以在一个Action执行前阻止其执行,同时也提供了 ...
- 过滤器(Filter)和拦截器(Interceptor)
过滤器(Filter) Servlet中的过滤器Filter是实现了javax.servlet.Filter接口的服务器端程序.它依赖于servlet容器,在实现上,基于函数回调,它可以对几乎所有请求 ...
- spring拦截器中修改响应消息头
问题描述 前后端分离的项目,前端使用Vue,后端使用Spring MVC. 显然,需要解决浏览器跨域访问数据限制的问题,在此使用CROS协议解决. 由于该项目我在中期加入的,主要负责集成shiro框架 ...
随机推荐
- MyBatis源码解析(三)——Transaction事务模块
原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/6634151.html 1.回顾 之前介绍了Environment环境类,这其实是一个单例类 ...
- Python作用域详述
作用域是指变量的生效范围,例如本地变量.全局变量描述的就是不同的生效范围. python的变量作用域的规则非常简单,可以说是所有语言中最直观.最容易理解的作用域. 在开始介绍作用域之前,先抛一个问题: ...
- MDK 中 [WEAK] 的作用
简介 __weak 或 [weak] 具有相同的功能,用于定义变量或者函数,常见于定义函数,在 MDK 链接时优先链接定义为非 weak 的函数或变量,如果找不到则再链接 weak 函数. 在STM3 ...
- 错误提示:The project was not built since its build path is incomplete. Cannot find the class file for java.lang.Object. Fix the build path then try building this project The type java.lang.Object cannot b
原文:http://www.cnblogs.com/mmzs/p/7662863.html 错误类型: 搞了很久才找到原因.解决办法写出来分享: 出现以上错误的原因是玩耍maven时多装了个jre.本 ...
- javascript小实例,在页面中输出当前客户端时间
时间对象(Date())比较简单,本文旨在为初学者入门使用,大牛可略过! 本文承接基础知识实例,说一下实例的要求: 在页面中输出当前客户端时间(2015年1月1日星期一10:10:10这样的格式),每 ...
- SQL SERVER PIVOT与用法解释
通俗简单的说:PIVOT就是行转列,UNPIVOT就是列传行 在数据库操作中,有些时候我们遇到需要实现“行转列”的需求,例如一下的表为某店铺的一周收入情况表: WEEK_INCOME(WEEK ),I ...
- VB.NET获取系统特殊目录
For Each x In GetType(System.Environment.SpecialFolder).GetEnumValues Debug.Print("{0} {1}" ...
- [linux] tcpdump抓包案例
1.常见参数 tcpdump -i eth0 -nn -s0 -v port 80 -i 选择监控的网卡 -nn 不解析主机名和端口号,捕获大量数据,名称解析会降低解析速度 -s0 捕获长度无限制 - ...
- Yii2基本概念之——事件(Event)
说起事件(event),我们可是一点都不陌生.现实生活当中的事件无处不在,比如你发了一条微博,触发了一条事件,导致关注你的人收到了一条消息,看到你发的内容:比如你通过支付宝买东西,付了款,触发一个事件 ...
- PHP Warning: PHP Startup: redis: Unable to initialize module Windows版本phpredis扩展
版权声明:经验之谈,不知能否换包辣条,另,转载请注明出处.https://www.cnblogs.com/zmdComeOn/category/1295248.html [root@VM_0_2_ce ...