1、定义拦截器
实现HandlerInterceptor
@Component
public class WxMiniInterceptor implements HandlerInterceptor { /*** @return 返回 true 放行、放回 false 拦截*/@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {log.info("进入微信小程序用户拦截器url={}" , request.getRequestURI());//过滤掉 确认的 options 请求if (HttpMethod.OPTIONS.name().equals(request.getMethod())) {return true;}String token = request.getHeader("token");if(StrUtil.isBlank(token)){ return false;}return true;}
}
2、加入拦截器配置
@Configuration
public class ResourcesConfig implements WebMvcConfigurer
{@Autowiredprivate RepeatSubmitInterceptor repeatSubmitInterceptor;@Autowiredprivate WxMiniInterceptor wxMiniInterceptor;@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry){/** 本地文件上传路径 */registry.addResourceHandler(Constants.RESOURCE_PREFIX + "/**").addResourceLocations("file:" + BhlConfig.getProfile() + "/");/** swagger配置 */registry.addResourceHandler("/swagger-ui/**").addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/");}/*** 自定义拦截规则*/@Overridepublic void addInterceptors(InterceptorRegistry registry){registry.addInterceptor(repeatSubmitInterceptor).addPathPatterns("/**");// 如果有多个拦截器,继续添加即可registry.addInterceptor(wxMiniInterceptor)// 放行.excludePathPatterns("/weixin/mini/login", "/weixin/mini/pay/notify")// 拦截.addPathPatterns("/weixin/mini/**");}
}