SpringBoot中拦截器和过滤器的使用
一、拦截器
三种方式
- 继承WebMvcConfigurerAdapter spring5.0 以弃用,不推荐
- 实现WebMvcConfigurer 推荐
- 继承WebMvcConfigurationSupport 会导致springboot自动配置失效
所以推荐使用第二种,方案,接口中的方法是default ,不必实现所有方法
代码:
import com.starfast.admin.interceptor.PermissionInterceptor;
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.WebMvcConfigurer; /**
* @author DUCHONG
**/
@Configuration
public class AdminWebConfig implements WebMvcConfigurer { @Autowired
private PermissionInterceptor permissionInterceptor; @Override
public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(permissionInterceptor)
.addPathPatterns("/**")
.excludePathPatterns("/monitor/druid/*")
.excludePathPatterns("/user/login")
.excludePathPatterns("/user/logout")
; }
}
二、过滤器
跨域过滤器
import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; public class CrossOriginFilter implements Filter {
public void destroy() {
System.out.println("----Filter销毁----");
} public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException { HttpServletResponse httpServletResponse = (HttpServletResponse)response;
httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
httpServletResponse.setHeader("Access-Control-Allow-Headers", "Authentication");
// 执行目标资源,放行
filterChain.doFilter(request, response);
} public void init(FilterConfig arg0) throws ServletException {
System.out.println("----Filter初始化----");
} }
添加到配置中,也可以和拦截器的配置放在一块
import com.starfast.admin.filter.CrossOriginFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import java.util.Arrays; /**
* 跨域过滤器
* @author DUCHONG
**/
@Configuration
public class ServletConfig { @Bean
public FilterRegistrationBean crossOriginFilter(){ FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean();
filterRegistrationBean.setFilter(new CrossOriginFilter());
filterRegistrationBean.setUrlPatterns(Arrays.asList("/router/**")); return filterRegistrationBean;
}
}
SpringBoot中拦截器和过滤器的使用的更多相关文章
- 面试题:struts 拦截器和过滤器
拦截器和过滤器的区别 过滤器是servlet规范中的一部分,任何java web工程都可以使用. 拦截器是struts2框架自己的,只有使用了struts2框架的工程才能用. 过滤器在url-patt ...
- Spring拦截器和过滤器
什么是拦截器 拦截器(Interceptor): 用于在某个方法被访问之前进行拦截,然后在方法执行之前或之后加入某些操作,其实就是AOP的一种实现策略.它通过动态拦截Action调用的对象,允许开发者 ...
- java 中的拦截器和过滤器
区别: 1.拦截器是基于java的反射机制的,而过滤器是基于函数回调 2.过滤器依赖与servlet容器,而拦截器不依赖与servlet容器 3.拦截器只能对action请求起作用,而过滤器则可以对几 ...
- Spring Boot2(七):拦截器和过滤器
一.前言 过滤器和拦截器两者都具有AOP的切面思想,关于aop切面,可以看上一篇文章.过滤器filter和拦截器interceptor都属于面向切面编程的具体实现. 二.过滤器 过滤器工作原理 从上图 ...
- springMVC拦截器和过滤器总结
拦截器: 用来对访问的url进行拦截处理 用处: 权限验证,乱码设置等 spring-mvc.xml文件中的配置: <beans xmlns="http://www.springfra ...
- java 拦截器和过滤器区别(转载)
1.拦截器是基于java的反射机制的,而过滤器是基于函数回调 2.过滤器依赖与servlet容器,而拦截器不依赖与servlet容器 3.拦截器只能对action请求起作用,而过滤器则可以对几乎所有的 ...
- SpringMVC学习笔记:拦截器和过滤器
首先说明一下二者的区别: 1. 拦截器基于java的反射机制,而过滤器是基于函数回调 2. 拦截器不依赖于servlet容器,过滤器依赖servlet容器 3. 拦截器只能对action请求起作用,而 ...
- Java Web 拦截器和过滤器的区别
一.AOP:面向切面编程,Java Web中有两个常用的技术:拦截器.过滤器 二.拦截器 1.定义:在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作 2.原理:大部分时候,拦截器方法都 ...
- Spring boot 拦截器和过滤器
1. 过滤器 Filter介绍 Filter可以认为是Servlet的一种“加强版”,是对Servlet的扩展(既可以对请求进行预处理,又可以对处理结果进行后续处理.使用Filter完整的一般流程是: ...
随机推荐
- Visual Studio UI Automation 学习(二)
今天恰好有时间,继续学习了一下UI Automation的知识.看了两篇博客,对UI Automation有了进一步的了解. https://blog.csdn.net/qq_37546891/art ...
- SLAM: SLAM的发展历程(WIKI)
参考维基百科: https://en.wikipedia.org/wiki/Simultaneous_localization_and_mapping 你们叫他SLAM,我还是习惯叫他三维重建.... ...
- yar 调用rpc方法
<?php class RpcController extends Yaf_Controller_Abstract { //RPC入口 public function indexAction($ ...
- myeclipse加载buiding workspace慢解决方案
最近做项目,每次保存修改的东西.myeclipse都会building workspace(重新编译)一下.并且那 building的速度真不够慢的啊. 严重影响编程速度. 在网上也发现遇到此问题的很 ...
- java中反射讲解及实例
Java反射机制详解 java 反射 定义 功能 示例 概要: Java反射机制详解 | |目录 1反射机制是什么 2反射机制能做什么 3反射机制的相关API ·通过一个对象获得完整的包名和类名 ·实 ...
- luogu P1714 切蛋糕 单调队列
单调队列傻题. 考虑以 $i$ 结尾的答案 : $max(sumv_{i}-sumv_{j}),j \in [i-m,i-1]$ ($sumv_{i}$ 为前缀和) 稍微搞一搞,发现 $sumv_{i ...
- 7.5 pragma 指令
第1个pragma禁用了我们感兴趣的特别警告,第2个恢复了它.禁用尽可能小的一段代码的警告是一个良好的做法,以便你不会错过任何真正应该修复的错误.如果你想在单独一行上禁用或恢复多个警告,那么只需用逗号 ...
- 渗透实战(周四):CSRF跨站域请求伪造
上图是广东外语外贸大学北校区内MBA中心旁边酒店房间的Wi-Fi网络环境,假设我们的Kali攻击机连入到SSID为414(房间号)的Wi-Fi网络,其IP地址:192.168.43.80 .同一Wi- ...
- 10.使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件(转)
出处:http://www.cnblogs.com/lichenwei/p/4145696.html Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由 ...
- BZOJ 2085 luogu P3502 [POI2010]Hamsters (KMP、Floyd、倍增)
数组开小毁一生-- 题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=2085 这题在洛谷上有个条件是"互不包含",其实 ...