springBoot 2.X-自定义拦截器
package com.cx.springboot.myInter; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView; /**
*
* @作者 陈先生
* @创建时间 2018年7月13日
* @功能描述 自定义拦截器
*/
@Configuration
public class MyInterceptor implements HandlerInterceptor { /**
* 在请求处理之前进行调用(Controller方法调用之前)
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
// 进行逻辑判断,如果ok就返回true,不行就返回false,返回false就不会处理改请求 String name = request.getParameter("name");
System.err.println(name +"-拦截器");
return true;
} /**
* 请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后)
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
//
}
/**
* 在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作)
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception { } }
1) 创建类 并实现 HandlerInterceptor 接口, 重写接口三个方法,具体方法执行时机见代码注释
package com.cx.springboot.myInter; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; /**
*
* @作者 陈先生
* @创建时间 2018年7月13日
* @功能描述 拦截器配置
*/
@Configuration
public class MvcConfig extends WebMvcConfigurationSupport {
// 以下WebMvcConfigurerAdapter 比较常用的重写接口
// /** 解决跨域问题 **/
// public void addCorsMappings(CorsRegistry registry) ;
// /** 添加拦截器 **/
// void addInterceptors(InterceptorRegistry registry);
// /** 这里配置视图解析器 **/
// void configureViewResolvers(ViewResolverRegistry registry);
// /** 配置内容裁决的一些选项 **/
// void configureContentNegotiation(ContentNegotiationConfigurer
// configurer);
// /** 视图跳转控制器 **/
// void addViewControllers(ViewControllerRegistry registry);
// /** 静态资源处理 **/
// void addResourceHandlers(ResourceHandlerRegistry registry);
// /** 默认静态资源处理器 **/
// void configureDefaultServletHandling(DefaultServletHandlerConfigurer
// configurer); @Autowired
private MyInterceptor myInterceptor; /**
* 表示这些配置的表示静态文件所处路径, 不用拦截
*/
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**")
.addResourceLocations("classpath:/static/");
registry.addResourceHandler("/templates/**")
.addResourceLocations("classpath:/templates/");
super.addResourceHandlers(registry);
} public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(myInterceptor)
// addPathPatterns 用于添加拦截规则 , 先把所有路径都加入拦截, 再一个个排除
.addPathPatterns("/**")
// excludePathPatterns 表示改路径不用拦截
.excludePathPatterns("/");
super.addInterceptors(registry);
}
}
2)配置拦截器 , 一般具体配置参数使用配置文件的方式配置
springBoot 2.X-自定义拦截器的更多相关文章
- springboot 2.0+ 自定义拦截器
之前项目的springboot自定义拦截器使用的是继承WebMvcConfigurerAdapter重写常用方法的方式来实现的. 以下WebMvcConfigurerAdapter 比较常用的重写接口 ...
- SpringBoot 2.x 自定义拦截器并解决静态资源访问被拦截问题
自定义拦截器 /** * UserSecurityInterceptor * Created with IntelliJ IDEA. * Author: yangyongkang * Date: ...
- SpringBoot中设置自定义拦截器
SpringBoot中设置自动以拦截器需要写一个类继承HandlerInterceptorAdapter并重写preHandle方法 例子 public class AuthorityIntercep ...
- SpringBoot自定义拦截器实现IP白名单功能
SpringBoot自定义拦截器实现IP白名单功能 转载请注明源地址:http://www.cnblogs.com/funnyzpc/p/8993331.html 首先,相关功能已经上线了,且先让我先 ...
- SpringMVC拦截器与SpringBoot自定义拦截器
首先我们先回顾一下传统拦截器的写法: 第一步创建一个类实现HandlerInterceptor接口,重写接口的方法. 第二步在XML中进行如下配置,就可以实现自定义拦截器了 SpringBoot实现自 ...
- SpringBoot学习笔记:自定义拦截器
SpringBoot学习笔记:自定义拦截器 快速开始 拦截器类似于过滤器,但是拦截器提供更精细的的控制能力,它可以在一个请求过程中的两个节点进行拦截: 在请求发送到Controller之前 在响应发送 ...
- SpringBoot(11) SpringBoot自定义拦截器
自定义拦截器共两步:第一:注册.第二:定义拦截器. 一.注册 @Configuration 继承WebMvcConfigurationAdapter(SpringBoot2.X之前旧版本) 旧版本代码 ...
- SpringBoot自定义拦截器实现
1.编写拦截器实现类,此类必须实现接口 HandlerInterceptor,然后重写里面需要的三个比较常用的方法,实现自己的业务逻辑代码 如:OneInterceptor package com ...
- 【学习】SpringBoot之自定义拦截器
/** * 自定义拦截器 **/ @Configuration//声明这是一个拦截器 public class MyInterceptor extends WebMvcConfigurerAdapte ...
- SpringBoot之自定义拦截器
一.自定义拦截器实现步骤 1.创建拦截器类并实现HandlerInterceptor接口 2.创建SpringMVC自定义配置类,实现WebMvcConfigurer接口中addInterceptor ...
随机推荐
- 第 18 章 Django 入门
当今的网站实际上都是富应用程序(rich application),就像成熟的桌面应用程序一样.Python提供了一组开发Web应用程序的卓越工具.在本章中,我们将学习如何使用Django(http: ...
- 小甲鱼C++笔记(上)1-24
一 OO思想:每个对象都是一个完整的独立的个体,由相关的属性和行为组合与外界分隔 OO思想的特点:1封装 把对象的属性和方法结合成一个独立的系统单位,并尽可能隐藏内部细节 2抽象 对一类公共问题进行统 ...
- Redis实战(四)
配置好了web.config程序,并且能通过C#代码来读取和管理以上配置信息. 接下来,就可以进行Redis的数据写入了.Redis中可以用Store和StoreAll分别保存单条和多条数据,C#中具 ...
- java -jar demo.jar
部署springboot项目 生成jar包其实还是依赖springboot的jar才能跑起来,为什么呢? 1.在C盘手工创建了一个文件夹,是拷贝了demo.jar这个jar包运行是报错的. 2.在D: ...
- python笔记六:进程与线程
1.进程 1)调用unix/linux系统中的进程函数fork(),用法和linux相同,调用成功返回0,失败返回-1: import os print 'Process (%s) start...' ...
- 深度学习应用系列(四)| 使用 TFLite Android构建自己的图像识别App
深度学习要想落地实践,一个少不了的路径即是朝着智能终端.嵌入式设备等方向发展.但终端设备没有GPU服务器那样的强大性能,那如何使得终端设备应用上深度学习呢? 所幸谷歌已经推出了TFMobile,去年又 ...
- AM335x内核模块驱动之LED
在Ubuntu的任意可操作的文件才建立text目录 在text中建立zyr-hello.c: #include<linux/kernel.h> #include<linux/modu ...
- T-sql 行转列,数据库查询分页
1 USE [APS_Future_FT] 2 GO 3 /****** Object: StoredProcedure [dbo].[A_CrudePrice] Script Date: 2013/ ...
- thunk 函数
function* f() { console.log(1); for (var i = 0; true; i++) { console.log('come in'); var reset = yie ...
- [NOIP2017]列队(线段树/裂点splay)
考虑n=1的做法,就是支持: 1.在线删一个数 2.在结尾加一个数 3.查询序列的第y个数 用线段树记录区间内被删元素的个数,可以通过线段树上二分快速得解,对于新增的数,用vector记录即可. 对于 ...