InterceptConfig.java 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package com.travel.config;
  2. import com.travel.util.JWTIntercept;
  3. import com.travel.util.SessionInterceptor;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.web.servlet.config.annotation.CorsRegistry;
  7. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  8. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  9. /**
  10. * 拦截器跟跨域解决 token拦截器
  11. */
  12. @Configuration
  13. public class InterceptConfig implements WebMvcConfigurer {
  14. @Autowired
  15. private SessionInterceptor sessionInterceptor;
  16. /**
  17. * 重写父类提供的跨域请求处理的接口
  18. *
  19. * @param registry
  20. */
  21. @Override
  22. public void addCorsMappings(CorsRegistry registry) {
  23. // 添加映射路径
  24. // registry.addMapping("/**")
  25. // // 放行哪些原始域
  26. // .allowedOriginPatterns("*")
  27. // .maxAge(3600)
  28. // // 是否发送Cookie信息
  29. // .allowCredentials(true)
  30. // // 放行哪些原始域(请求方式)
  31. // .allowedMethods("*")
  32. // // 放行哪些原始域(头部信息)
  33. // .allowedHeaders("*")
  34. // // 暴露哪些头部信息(因为跨域访问默认不能获取全部头部信息)
  35. // .exposedHeaders("Server", "Content-Length", "Authorization", "Access-Token", "Access-Control-Allow-Origin", "Access-Control-Allow-Credentials");
  36. registry.addMapping("/**")
  37. .allowedHeaders("*")
  38. .allowedMethods("*")
  39. .allowCredentials(true)
  40. .allowedOriginPatterns("*");
  41. }
  42. @Override
  43. public void addInterceptors(InterceptorRegistry registry) {
  44. registry.addInterceptor(sessionInterceptor);// 多次请求监听拦截
  45. registry.addInterceptor(new JWTIntercept())
  46. .addPathPatterns("/**") //拦截所有请求/**
  47. .excludePathPatterns("/personal/*").excludePathPatterns("/supplier/*"); //放心登录接口,因为要通过登录获取token
  48. }
  49. }