过滤器执行顺序

路由过滤器、defaultFilter、全局过滤器的执行顺序?

①order值越小,优先级越高

②当order值一样时,顺序是defaultFilter最先,然后是局部的路由过滤器,最后是全局过滤器

  • 每一个过滤器都必须指定一个int类型的order值,order**值越小,优先级越高,执行顺序越靠前。
  • GlobalFilter通过实现Ordered接口,或者添加@Order注解来指定order值,由我们自己指定
  • 路由过滤器和defaultFilter的order由Spring指定,默认是按照声明顺序从1递增
  • 当过滤器的order值一样时,会按照 defaultFilter > 路由过滤器 > GlobalFilter的顺序执行。、
1
2
3
4
org.springframework.cloud.gateway.route.RouteDefinitionRouteLocator#getFilters()方法是先加载defaultFilters,然后再加载某个route的filters,然后合并。

org.springframework.cloud.gateway.handler.FilteringWebHandler#handle()方法会加载全局过滤器,与前面的过滤器合并后根据order排序,组织过滤器链

跨域问题处理

  • 跨域:域名不一致就是跨域,主要包括:

    • 域名不同: www.taobao.com 和 www.taobao.org 和 www.jd.com 和 miaosha.jd.com

    • 域名相同,端口不同:localhost:8080和localhost8081

  • 跨域问题:浏览器禁止请求的发起者与服务端发生跨域ajax请求,请求被浏览器拦截的问题

  • 解决方案:CORS

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    globalcors:
    add-to-simple-url-handler-mapping: true
    cors-configurations:
    '[/**]':
    allowedOrigins: # 允许哪些网站的跨域请求
    - "http://localhost:8090"
    - "http://www.leyou.com"
    allowedMethods: # 允许跨域的ajax的请求方式
    - "GET"
    - "POST"
    - "DELETE"
    - "PUT"
    - "OPTIONS"
    allowedHeaders: "*" # 允许在请求中携带的头信息
    allowCredentials: true #是否允许跨域携带cookie
    maxAge: 360000 #这次跨域检查的有效期