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. csp2022第一轮游记

    DAY -7? 学校没买桶装水!我一时半会不去打水,真的渴.果不其然开始咳嗽了.DAY -1 隔壁班同学主动申请停课了,我也跟来复习,这天主要的成果是把选择题错误控制到2-3题,顺便整理了一点笔记. ...

  2. 【网络】博客网站搭建之Typecho(命令版)

    目录 前言 个人博客系统筛选 内网穿透 安装nginx 安装PHP 安装mysql Typecho 环境安装 参考 安装typecho Nginx与PHP进行连接配置&指定博客路径 验证 配置 ...

  3. windows安装wsl,在windows中使用ubuntu

    WSL(Windows Subsystem for Linux)即适用于 Linux 的 Windows 子系统,它是随 Windows 操作系统一起提供. WSL是windows操作系统的子系统,算 ...

  4. Project facet Java version 13 is not supported.

    问题 导入的文件运行时出现报错:Project facet Java version 13 is not supported. 大概就是版本不支持,看了下自己的Java版本是1.8的,修改下版本即可运 ...

  5. Froms

    首先看到的是一个输入框 不多说,直接bp抓下来 然后传repeater里,发现了pin值后showsource值,pin值没什么,应该是做题用的,而showsource是个隐藏的值,将其0改为1后go ...

  6. ES文件传输助手1.0.0

    软件下载地址 1.软件功能 与 ES文件浏览器 的快传功能 直接传输文件 支持接受文件点击预览 可以多台电脑使用该软件,从而实现电脑与电脑局域网互传文件 单个文件夹上传会递归上传该文件夹下所有文件夹与 ...

  7. java面试题-常用框架

    Spring Spring 是什么 一个开发框架,一个容器,主要由面向切面AOP 和依赖注入DI两个方面,外加一些工具 AOP和IOC AOP 面向切面 AOP是一种编程思想,主要是逻辑分离, 使业务 ...

  8. Linux命令第三部分

    一.命令 1.mv命令 ·不更改文件路径 改名 ·更改文件路径 剪切 mv  [选项]  源文件或目录   目标文件或目录 2.which 查找命令.文件存放目录 搜索范围由环境变量PATH决定 3. ...

  9. 常用BOM操作 DOM操作 事件 jQuery类库

    目录 BOM操作 常用BOM操作 三种弹出框 alert confirm prompt 定时任务 setTimeout 循环定时 setInterval DOM操作 查找标签 直接查找 间接查找 操作 ...

  10. MySQL数据结构(索引)

    目录 一:MySQL索引与慢查询优化 1.什么是索引? 2.索引类型分类介绍 3.不同的存储引擎支持的索引类型也不一样 二:索引的数据结构 1.二叉树(每个节点只能分两个叉) 2.数据结构(B树) 3 ...