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);
    }
}