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. 【Virt.Contest】CF1215(div.2)

    第二次打虚拟赛. CF 传送门 T1:Yellow Cards 黄色卡片 中规中矩的 \(T1\). 首先可以算出一个人也不罚下时发出的最多黄牌数: \(sum=a1*(k1-1)+a2*(k2-1) ...

  2. Installing ClickHouse-22.10.2.11 on openEuler

    一.Installing ClickHouse-22.10.2.11 on openEuler 1 地址 https://clickhouse.com https://packages.clickho ...

  3. 13、设计一个函数process,在你调用他的时候,每次实现不同的功能,输入a,b两个数, 第一次调用时找出a,b中的最大者。 第二次找出最小者,,第三次求两个数的和。

    /* 设计一个函数process,在你调用他的时候,每次实现不同的功能,输入a,b两个数, 第一次调用时找出a,b中的最大者. 第二次找出最小者,,第三次求两个数的和. */ #include < ...

  4. 如何使用虚拟机下载和安装Centos

    首先先下载虚拟机进行安装我这里使用的是 VMware Workstation Player 当前版本为16.这个是免费使用的可以不需要许可证就能长期使用了 官网VMware Workstation P ...

  5. 关于deepin-wine或wine更换字体方法

    前言 首先要知道,deepin-wine打包的QQ和你自己用 deepin-wine跑的windows软件,他们所在不是同一个容器 deepin打包QQ所在的容器,在你的 ~/.deepinwine ...

  6. java 如何正确使用接口返回对象Result

    1. Result的使用 Result的使用,是java项目中开发接口的必备,它经常被我们用作接口的返回对象,方便前端或者其他程序的远程调用后处理业务.它一般包括以下几个属性: code:一般根据系统 ...

  7. Linux通过脚本实现多台主机统一部署

    该脚本分成三部分,一部分是获取信息的脚本:getInfo.sh 一个是main脚本:main.sh.一个是ssh连接主机脚本:sshing.sh main.sh #是否检查主机是否存活host_che ...

  8. vue中点按钮回到顶部,和elementUI中置顶按钮的实现

    一般做pc项目都会遇到侧边小按钮点击回到顶部的功能,现在记录一下项目中的实现方法 一.结合动画效果实现 <li class="defa_bt" @click="hd ...

  9. JavaScript:输出语法

    主要有三种,如下所示:

  10. STL map容器常用API

    map容器:键值和实值是分开的,排序规则按照键值排序 #define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<map& ...