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 拦截器的使用的更多相关文章

  1. SpringBoot自定义拦截器实现IP白名单功能

    SpringBoot自定义拦截器实现IP白名单功能 转载请注明源地址:http://www.cnblogs.com/funnyzpc/p/8993331.html 首先,相关功能已经上线了,且先让我先 ...

  2. SpringBoot使用拦截器

    SpringBoot的拦截器只能拦截流经DispatcherServlet的请求,对于自定义的Servlet无法进行拦截. SpringMVC中的拦截器有两种:HandlerInterceptor和W ...

  3. SpringBoot 注册拦截器方式及拦截器如何获取spring bean实例

    SpringBoot 注册拦截器时,如果用New对象的方式的话,如下: private void addTokenForMallInterceptor(InterceptorRegistry regi ...

  4. springboot+springmvc拦截器做登录拦截

    springboot+springmvc拦截器做登录拦截 LoginInterceptor 实现 HandlerInterceptor 接口,自定义拦截器处理方法 LoginConfiguration ...

  5. SpringMVC拦截器与SpringBoot自定义拦截器

    首先我们先回顾一下传统拦截器的写法: 第一步创建一个类实现HandlerInterceptor接口,重写接口的方法. 第二步在XML中进行如下配置,就可以实现自定义拦截器了 SpringBoot实现自 ...

  6. springboot + 注解 + 拦截器 + JWT 实现角色权限控制

    1.关于JWT,参考: (1)10分钟了解JSON Web令牌(JWT) (2)认识JWT (3)基于jwt的token验证 2.JWT的JAVA实现 Java中对JWT的支持可以考虑使用JJWT开源 ...

  7. SpringBoot中拦截器和过滤器的使用

    一.拦截器 三种方式 继承WebMvcConfigurerAdapter   spring5.0 以弃用,不推荐 实现WebMvcConfigurer  推荐 继承WebMvcConfiguratio ...

  8. 【SpringBoot】拦截器使用@Autowired注入接口为null解决方法

    最近使用SpringBoot的自定义拦截器,在拦截器中注入了一个DAO,准备下面作相应操作,拦截器代码: public class TokenInterceptor implements Handle ...

  9. springBoot 使用拦截器 入坑

    近期使用SpringBoot 其中用到了拦截器 结果我的静态资源被全部拦截了,让我导致了好久才搞好: 看下图项目结构: 问题描述:上图划红框的资源都被拦截器给拦截了,搞得项目中不能访问:解决问题就是在 ...

  10. springboot的拦截器Interceptor的性质

    Interceptor在springboot2.x版本的快速入门 实现HandlerInterceptor的接口,并重载它的三个方法:preHandle.postHandle.afterComplet ...

随机推荐

  1. SQLSever数据库基本操作

    一.SQLSever数据库基本操作 1.创建数据库 use master if exists(select * from sysdatabases where name='SMDB') drop da ...

  2. 2022春每日一题:Day 11

    题目:高斯消元法 高斯消元法是一个模板,下面简单介绍其内容以及实现方法. 高斯消元是求一个求多元一次方程组的解的算法. 就是形式如下的关于x1,x2...xn的方程组的解. a11x1 + a12x2 ...

  3. 编码工具使用(go语言)

    1.课程介绍 Git基础课程和实操 Goland介绍以及常用快捷键使用 Go delve 调试 你想要的linux 这里都有 2.版本控制工具介绍 原始的版本控制 修改文件,保存文件副本 版本控制的起 ...

  4. 2018 Web开发人员学习路线图

    以下 Web 开发人员学习路线图是来自 Github developer-roadmap 项目,目前已经有繁体版翻译 developer-roadmap-chinese. 主要有三个方向,分别为前端开 ...

  5. Kubernetes—资源管理

    3. 资源管理 3.1 资源管理介绍 在kubernetes中,所有的内容都抽象为资源,用户需要通过操作资源来管理kubernetes. kubernetes的本质上就是一个集群系统,用户可以在集群中 ...

  6. Windows自带管理工具

    exe类notepad 记事本 control 控制面板 mstsc 远程桌面连接explorer 资源管理器 taskmgr 任务管理器resmon 资源监视器 perfmon 性能监视器reged ...

  7. 基于python的数学建模---机场航线设计

    数据集 拿走: 链接:https://pan.baidu.com/s/1zH5xhpEmx2_u5qO9W4gCkw 提取码:2wl5 数据集来自航空业,有一些关于航线的基本信息.有某段旅程的起始点和 ...

  8. SSH(二)框架配置文件

    在引入了宽假所需要的jar包后,引入相应配置文件. 一.Struts2的配置文件: 1.Struts2的黑心过滤器,在web.xml中引入: <!-- struts2框架的核心过滤器  clas ...

  9. Python异步协程(asyncio详解)

    续上篇讲解yield from博客,上篇链接:https://www.cnblogs.com/Red-Sun/p/16889182.html PS:本博客是个人笔记分享,不需要扫码加群或必须关注什么的 ...

  10. k8s篇-k8s集群架构及组件详解【史上最详细】

    O kubernetes简介 k8s是什么 k8s是一个可移植的.可扩展的开源平台,用于管理容器化的工作负载和服务,可以促进声明式配置和自动化. k8s能做什么 1)服务发现和负载均衡 Kuberne ...