自定义拦截器Interceptor
自定义拦截器
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;@Component
public class TimeInterceptor implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {System.out.println("prehandle");return true;}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,ModelAndView modelAndView) throws Exception {System.out.println("postHandle");}@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)throws Exception {System.out.println("afterCompletion");}
}
自定义WebConfig
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;import com.ztx.web.interceptor.TimeInterceptor;
@Configuration
public class WebConfig implements WebMvcConfigurer {//WebMvcConfigurer接口的方式,而不用使用WebMvcConfigurerAdapter @Autowiredprivate TimeInterceptor timeInterceptor;//自定义的拦截器,标注为@compent已经注入带bean中@Overridepublic void addInterceptors(InterceptorRegistry registry) {//registry.addInterceptor(timeInterceptor).addPathPatterns("/user");;//添加登录处理拦截器,拦截所有请求,登录请求除外InterceptorRegistration interceptorRegistration = registry.addInterceptor(timeInterceptor);//排除配置/*interceptorRegistration.excludePathPatterns("/sys/login.json");interceptorRegistration.excludePathPatterns("/charts/**");interceptorRegistration.excludePathPatterns("/css/**");interceptorRegistration.excludePathPatterns("/easyUi/**");interceptorRegistration.excludePathPatterns("/flashPlayer/**");interceptorRegistration.excludePathPatterns("/font/**");interceptorRegistration.excludePathPatterns("/images/**");interceptorRegistration.excludePathPatterns("/js/**");interceptorRegistration.excludePathPatterns("/pages/**");interceptorRegistration.excludePathPatterns("/plugin/**");interceptorRegistration.excludePathPatterns("/index.html");interceptorRegistration.excludePathPatterns("/show.html");*///配置拦截策略interceptorRegistration.addPathPatterns("/**");}/* @Beanpublic FilterRegistrationBean timeFilter() {FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();TimeFilter TimeFilter = new TimeFilter();filterRegistrationBean.setFilter(TimeFilter);List<String> url = new ArrayList<String>();url.add("/*");filterRegistrationBean.setUrlPatterns(url);return filterRegistrationBean;}*/}
自定义拦截器Interceptor相关推荐
- 【Spring MVC】自定义拦截器 Interceptor
一.自定义拦截器 1.拦截器,跟过滤器比较像的技术. 1.1 拦截器和过滤器的区别: 拦截器只能拦Controller,Filter可以拦任何请求 因为Spring的入口是dispatcherServ ...
- struts2自定义拦截器一——模拟登陆权限验证
1.http://localhost:8083/struts2/user.jsp 表示用户已登陆,存放session对象 2.http://localhost:8083/struts2/quit.js ...
- 使用mybatis plus自定义拦截器,实现数据权限
需求 为了增强程序的安全性,需要在用户访问数据库的时候进行权限判断后选择性进行判断是否需要增强sql,来达到限制低级别权限用户访问数据的目的. 根据业务需要,这里将角色按照数据范围做权限限定.比如,角 ...
- Hadoop生态圈-Flume的组件之自定义拦截器(interceptor)
Hadoop生态圈-Flume的组件之自定义拦截器(interceptor) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客只是举例了一个自定义拦截器的方法,测试字节传输速 ...
- java interceptor用法_java自定义拦截器用法实例
本文实例讲述了java自定义拦截器及其用法.分享给大家供大家参考.具体如下: LoginInterceptor.java文件如下: package com.tq365.util; import jav ...
- web开发(二十一)之自定义拦截器的使用
转自博客:http://blog.csdn.net/pkgk2013/article/details/51985817 拦截器的作用 拦截器,在AOP(Aspect-Oriented Programm ...
- Flume-NG源码阅读之SourceRunner,及选择器selector和拦截器interceptor的执行
在AbstractConfigurationProvider类中loadSources方法会将所有的source进行封装成SourceRunner放到了Map<String, SourceRun ...
- Struts2拦截器(Interceptor)原理详解
1. 理解拦截器 1.1. 什么是拦截器: 拦截器,在AOP(Aspect-Oriented Programming)中用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作 ...
- Spring MVC 拦截器 interceptor 详解
Spring MVC-拦截器 今天就是把有关拦截器的知识做一个总结. 1.拦截器概述 1.1 什么是拦截器? Spring MVC中的拦截器(Interceptor)类似于Servlet中的过滤器(F ...
最新文章
- python loop until_非常适合小白的 Asyncio 教程
- python 如何将字符串数字列表转换成数字列表,如何将数字列表转换成字符串数字列表?map(eval,list(str))
- Qt 检测第三方软件是否运行、杀死第三方进程
- 安装ubuntu系统步骤
- 微信群有人发广告?用 Python 写自动化机器人消灭他!
- linux手术后10年,经历正颌手术10年后遗症的我想说40岁做正颌手术我很后悔
- 【LEDE】树莓派上玩LEDE终极指南-92-自己编译的LEDE为啥子不能用SSR和KoolProxy?
- VLAN、OSPF、GRE或IPSEC配置作业与抓包内容(新手入门)
- LaTeX数学公式-详细教程
- 软件运维监控有哪些?
- .mat转.tif 用于arcgis裁剪遥感图像
- 微信小程序开发教程——1.0.1appid注册和开发者工具下载
- 计算机网络和智能家居,智能家居与传统智能家居到底有什么区别
- thrift文件服务器,Thrift功能和使用
- MT7686芯片资料手册
- 多模态生物特征识别 基于人脸与人耳信息pdf
- 蓝牙防水耳机排行榜前十名,防水音质表现好的蓝牙耳机推荐
- 从键盘输入一个三位整数n,分别求出n的个位数字、十位数字和百位数字
- 电线线缆铜芯和铝芯有什么区别?哪个更好呢?
- 1.16. BIP39协议:使用助记词生成确定性钱包