InterceptConfig.java 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package com.supplier.config;
  2. import com.supplier.util.JWTIntercept;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.web.cors.CorsConfiguration;
  6. import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
  7. import org.springframework.web.filter.CorsFilter;
  8. import org.springframework.web.servlet.config.annotation.CorsRegistry;
  9. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  10. import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
  11. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  12. @Configuration
  13. public class InterceptConfig implements WebMvcConfigurer {
  14. /**
  15. * 重写父类提供的跨域请求处理的接口
  16. *
  17. * @param registry
  18. */
  19. @Override
  20. public void addCorsMappings(CorsRegistry registry) {
  21. // 添加映射路径
  22. registry.addMapping("/**")
  23. // 放行哪些原始域
  24. .allowedOriginPatterns("*")
  25. // 是否发送Cookie信息
  26. .allowCredentials(true)
  27. // 放行哪些原始域(请求方式)
  28. .allowedMethods("GET", "POST", "DELETE", "PUT", "OPTIONS", "HEAD")
  29. // 放行哪些原始域(头部信息)
  30. .allowedHeaders("*")
  31. // 暴露哪些头部信息(因为跨域访问默认不能获取全部头部信息)
  32. .exposedHeaders("Server", "Content-Length", "Authorization", "Access-Token", "Access-Control-Allow-Origin", "Access-Control-Allow-Credentials");
  33. }
  34. private CorsConfiguration buildConfig() {
  35. CorsConfiguration corsConfiguration = new CorsConfiguration();
  36. corsConfiguration.addAllowedOrigin("*"); // 1 设置访问源地址
  37. corsConfiguration.addAllowedHeader("*"); // 2 设置访问源请求头
  38. corsConfiguration.addAllowedMethod("*"); // 3 设置访问源请求方法
  39. return corsConfiguration;
  40. }
  41. @Bean
  42. public CorsFilter corsFilter() {
  43. UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
  44. source.registerCorsConfiguration("/**", buildConfig()); // 4 对接口配置跨域设置
  45. return new CorsFilter(source);
  46. }
  47. @Override
  48. public void addInterceptors(InterceptorRegistry registry) {
  49. registry.addInterceptor(new JWTIntercept())
  50. .addPathPatterns("/**") // 正常情况下 拦截所有请求/**,因测试需要就先拦截/user/test
  51. .excludePathPatterns("/", "/wx/login/**", "/page/*", "/js/**", "/i18n/**", "/css/**", "/images/**", "/layui/**", "/json/**" ,"/remixicon/**", "/wxpay/**","/appPay/**", "http://a1-v2.easemob.com/**", "http://39.97.9.52/*", "http://47.95.246.247/**", "ws://39.102.159.61/**", "/page/registered/**"); //放心登录接口,因为要通过登录获取token
  52. }
  53. }