springboot FilterRegistrationBean 拦截器的使用
1.创建一个filter
package com.ruoyi.weixin.user.interator; import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.weixin.user.domain.vo.UserWeixinInfo;
import com.ruoyi.weixin.user.service.impl.WxTokenService;
import org.springframework.beans.factory.annotation.Autowired; import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException; public class MyWeiXinInterceptor implements Filter { @Autowired
private WxTokenService wxtokenService; @Override
public void init(FilterConfig filterConfig) throws ServletException
{ } @Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException
{
UserWeixinInfo info = wxtokenService.getUserWeixinInfo((HttpServletRequest)request);
if (StringUtils.isNotNull(info) && StringUtils.isNull(SecurityUtils.getAuthentication()))
{ wxtokenService.verifyToken(info);
chain.doFilter(request, response);
}
return;
} @Override
public void destroy()
{ } }
2.配置filter(方法1推荐使用)
package com.ruoyi.weixin.user.config; import com.ruoyi.common.filter.RepeatableFilter;
import com.ruoyi.weixin.user.interator.MyWeiXinInterceptor;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class WeiXinFilterConfig { @SuppressWarnings({ "rawtypes", "unchecked" })
@Bean
public FilterRegistrationBean weixinUrlFilterRegistration()
{
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new MyWeiXinInterceptor()); //要添加的拦截器
registration.addUrlPatterns("/**/lvju/*"); //拦截路径
registration.setName("weixinUrlFilter"); //设置拦截器名称
registration.setOrder(FilterRegistrationBean.LOWEST_PRECEDENCE); //排序
return registration;
}
//如果要添加多个拦截器,把上面的方法改一下就行
@SuppressWarnings({ "rawtypes", "unchecked" })
@Bean
public FilterRegistrationBean2 weixinUrlFilterRegistration()
{
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new MyWeiXinInterceptor2()); //要添加的拦截器
registration.addUrlPatterns("/**/lvju/*"); //拦截路径
registration.setName("weixinUrlFilter2"); //设置拦截器名称
registration.setOrder(FilterRegistrationBean.LOWEST_PRECEDENCE); //排序
return registration;
}
}
3.配置filter(方法2)
@Configuration
public class ResourcesConfig implements WebMvcConfigurer
{
@Autowired
private MyWeiXinInterceptor2 myWeiXinInterceptor2; @Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
{
/** 本地文件上传路径 */
registry.addResourceHandler(Constants.RESOURCE_PREFIX + "/**").addResourceLocations("file:" + RuoYiConfig.getProfile() + "/"); /** swagger配置 */
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
} /**
* 自定义拦截规则
*/
@Override
public void addInterceptors(InterceptorRegistry registry)
{
registry.addInterceptor(myWeiXinInterceptor2).addPathPatterns("/**/lvju/*");
} /**
* 跨域配置
*/
@Bean
public CorsFilter corsFilter()
{
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
// 设置访问源地址
config.addAllowedOrigin("*");
// 设置访问源请求头
config.addAllowedHeader("*");
// 设置访问源请求方法
config.addAllowedMethod("*");
// 对接口配置跨域设置
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
}
springboot FilterRegistrationBean 拦截器的使用的更多相关文章
- SpringBoot自定义拦截器实现IP白名单功能
SpringBoot自定义拦截器实现IP白名单功能 转载请注明源地址:http://www.cnblogs.com/funnyzpc/p/8993331.html 首先,相关功能已经上线了,且先让我先 ...
- SpringBoot使用拦截器
SpringBoot的拦截器只能拦截流经DispatcherServlet的请求,对于自定义的Servlet无法进行拦截. SpringMVC中的拦截器有两种:HandlerInterceptor和W ...
- SpringBoot 注册拦截器方式及拦截器如何获取spring bean实例
SpringBoot 注册拦截器时,如果用New对象的方式的话,如下: private void addTokenForMallInterceptor(InterceptorRegistry regi ...
- springboot+springmvc拦截器做登录拦截
springboot+springmvc拦截器做登录拦截 LoginInterceptor 实现 HandlerInterceptor 接口,自定义拦截器处理方法 LoginConfiguration ...
- SpringMVC拦截器与SpringBoot自定义拦截器
首先我们先回顾一下传统拦截器的写法: 第一步创建一个类实现HandlerInterceptor接口,重写接口的方法. 第二步在XML中进行如下配置,就可以实现自定义拦截器了 SpringBoot实现自 ...
- springboot + 注解 + 拦截器 + JWT 实现角色权限控制
1.关于JWT,参考: (1)10分钟了解JSON Web令牌(JWT) (2)认识JWT (3)基于jwt的token验证 2.JWT的JAVA实现 Java中对JWT的支持可以考虑使用JJWT开源 ...
- SpringBoot中拦截器和过滤器的使用
一.拦截器 三种方式 继承WebMvcConfigurerAdapter spring5.0 以弃用,不推荐 实现WebMvcConfigurer 推荐 继承WebMvcConfiguratio ...
- 【SpringBoot】拦截器使用@Autowired注入接口为null解决方法
最近使用SpringBoot的自定义拦截器,在拦截器中注入了一个DAO,准备下面作相应操作,拦截器代码: public class TokenInterceptor implements Handle ...
- springBoot 使用拦截器 入坑
近期使用SpringBoot 其中用到了拦截器 结果我的静态资源被全部拦截了,让我导致了好久才搞好: 看下图项目结构: 问题描述:上图划红框的资源都被拦截器给拦截了,搞得项目中不能访问:解决问题就是在 ...
- springboot的拦截器Interceptor的性质
Interceptor在springboot2.x版本的快速入门 实现HandlerInterceptor的接口,并重载它的三个方法:preHandle.postHandle.afterComplet ...
随机推荐
- perl中ENV的使用
在打印环境变量的时候可以用到.实际上是%ENV,perl中的哈希变量,里面保存的是环境变量.键是环境变量名,值是环境变量值.例如,有一个环境变量是PATH,其值为C:\windows,那么,打印这个环 ...
- Optional对象
Optional对象 Optional 类是一个可以为null的容器对象,用于简化Java中对空值的判断处理,以防止出现各种空指针异常. 静态方法-of 必须确定对象不为null 在使用of封装成op ...
- Ian Lance Taylor
https://img.mukewang.com/5a9dfda50001933e23006728.png 在GCC的世界中,没有人比Ian更火.在GCC maillist中,Ian的身影呈现在前端中 ...
- openssh编译rpm包(防火防盗防漏扫)
参考链接:https://www.jianshu.com/p/0882b0502960 openssh下载链接: wget https://cdn.openbsd.org/pub/OpenBSD/Op ...
- python进阶(29)单例模式
初识单例模式 单例模式含义 单例模式,也叫单子模式,是一种常用的软件设计模式.在应用这个模式时,单例对象的类必须保证只有一个实例存在.许多时候整个系统只需要拥有一个的全局对象,这样有利于我们协调系统整 ...
- C温故补缺(七):函数指针与回调函数
函数指针与回调函数 函数指针就是指向函数调用栈地址的指针,定义时须和函数的返回值类型,参数类型相同 如: #include<stdio.h> int max(int x,int y){ r ...
- c++ trivial, standard layout和POD类型解析
目录 1. trivial类型 2. standard layout类型 3. 集大成者,POD(Plain Old Data)类型 4. 测试代码 1. trivial类型 占用一片连续的内存,编译 ...
- 数电第一周总结_by_yc
数电第一周总结 重点:Verilog建模方式 结构级建模: 需基于电路原理图 module mux( input data0, input data1, input sel, output out); ...
- .net core 中 WebApiClientCore的使用
WebApiClient 接口注册与选项 1 配置文件中配置HttpApiOptions选项 配置示例 "IUserApi": { "HttpHost": &q ...
- python Flask 操作数据库
Flask数据库 转载:Flask数据库 - 苦行僧95 - 博客园 (cnblogs.com) Flask-SQLAlchemy Flask-SQLAlchemy是在Flask中操作关系型数据库的拓 ...