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