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)简介的更多相关文章

  1. web 过滤器 Filter、 Spring 拦截器 interceptor

    1.过滤器(Filter)(在web.xml中注册过滤器) 首先说一下Filter的使用地方,我们在配置web.xml时,总会配置下面一段设置字符编码,不然会导致乱码问题: <filter> ...

  2. spring拦截器Interceptor

    在Spring Boot中,拦截器可以分为两种类型: 一是WebMVC,负责拦截请求,类似于过滤器,对用户的请求在Controller接收前进行处理,在Controller处理完成后加工结果等.使用时 ...

  3. spring 拦截器简介

    spring 拦截器简介 常见应用场景 1.日志记录:记录请求信息的日志,以便进行信息监控.信息统计.计算PV(Page View)等.2.权限检查:如登录检测,进入处理器检测检测是否登录,如果没有直 ...

  4. Spring中过滤器(Filter)和拦截器(Interceptor)的区别和联系

    在我们日常的开发中,我们经常会用到Filter和Interceptor.有时同一个功能.Filter可以做,Interceptor也可以做.有时就需要考虑使用哪一个比较好.这篇文章主要介绍一下,二者的 ...

  5. 5-21 拦截器 Interceptor

    Spring MVC拦截器 什么是拦截器 拦截器是SpringMvc框架提供的功能 它可以在控制器方法运行之前或运行之后(还有其它特殊时机)对请求进行处理或加工的特定接口 常见面试题:过滤器和拦截器的 ...

  6. Spring拦截器中通过request获取到该请求对应Controller中的method对象

    背景:项目使用Spring 3.1.0.RELEASE,从dao到Controller层全部是基于注解配置.我的需求是想在自定义的Spring拦截器中通过request获取到该请求对应于Control ...

  7. java之拦截器Interceptor

    1,拦截器的概念    java里的拦截器是动态拦截Action调用的对象,它提供了一种机制可以使开发者在一个Action执行的前后执行一段代码,也可以在一个Action执行前阻止其执行,同时也提供了 ...

  8. 过滤器(Filter)和拦截器(Interceptor)

    过滤器(Filter) Servlet中的过滤器Filter是实现了javax.servlet.Filter接口的服务器端程序.它依赖于servlet容器,在实现上,基于函数回调,它可以对几乎所有请求 ...

  9. spring拦截器中修改响应消息头

    问题描述 前后端分离的项目,前端使用Vue,后端使用Spring MVC. 显然,需要解决浏览器跨域访问数据限制的问题,在此使用CROS协议解决. 由于该项目我在中期加入的,主要负责集成shiro框架 ...

随机推荐

  1. springboot情操陶冶-SpringApplication(二)

    承接前文springboot情操陶冶-SpringApplication(一),本文将对run()方法作下详细的解析 SpringApplication#run() main函数经常调用的run()方 ...

  2. Django学习笔记(3)——表单,测试和模板语法的学习

    一,表单form 为了接收用户的投票选择,我们需要在前段页面显示一个投票界面,让我们重写之前的polls/detail.html文件,代码如下: <h1>{{ question.quest ...

  3. 完整例子-正则控制input的输入

    转 : https://www.cnblogs.com/ckf1988/p/5619337.html

  4. 使用LayoutInflater添加一个布局引用

    LayoutInflater 与 xml 的<include/>的区别,至今没搞清楚,下面记录一下LayoutInflater引用一个布局并立即显示呈现的方法: private void ...

  5. [转]windows BLE编程 net winform 连接蓝牙4.0

    本文转自:https://www.cnblogs.com/webtojs/p/9675956.html winform 程序调用Windows.Devices.Bluetoot API 实现windo ...

  6. C#静态成员静态类。

    1. 在静态类中,所包含的所有成员必须是静态的.但不是所有静态成员都必须写在静态类中.       实例成员属于具体的对象,静态成员是属于类的.所以访问静态成员的时候不能通过对象来访问,只能通过类名直 ...

  7. petapoco 实体中字段去掉关联(类似于EF中的NotMap)

    怎么才能让不是数据库表中的字段放在实体中而不影响正常的插入和更新呢? 找到 PetaPoco.cs 文件,打开之后,搜索插入方法(Insert),然后继续找到下一层方法 就能看到如下代码: 看到这个注 ...

  8. Android Studio 学习(五)网络

    HttpURLConnection OkHttp 添加依赖 编辑 app/build.gradle 在dependencies闭包中添加 implementation 'com.squareup.ok ...

  9. 设计模式之原型模式——Java语言描述

    原型模式是用于创建重复对象,同时又能保证性能.这种类型的设计模式属于创建型模式,它提供了一种创建对象的方式 这种模式实现了一个原型接口,该接口用于创建当前对象的克隆.当直接创建对象的代价比较大时,则适 ...

  10. 程序员晋级CTO之路的8大准则

    推荐阅读: 大数据智慧平台落地方案 Nginx + 阿里云SSL + tomcat 实现https访问代理 永远别忘了TD 再确认测试代码前,先找别人帮你检查下是否无误.在别人做之前尽量检查出bug并 ...