Spring Cloud实战小贴士:Zuul处理Cookie和重定向

开发 开发工具
由于我们在之前所有的入门教程中,对于HTTP请求都采用了简单的接口实现。而实际使用过程中,我们的HTTP请求要复杂的多,本文主要分析HTTP请求时遇见的常见问题以及解决方法。

由于我们在之前所有的入门教程中,对于HTTP请求都采用了简单的接口实现。而实际使用过程中,我们的HTTP请求要复杂的多,比如当我们将Spring Cloud Zuul作为API网关接入网站类应用时,往往都会碰到下面这两个非常常见的问题:

  • 会话无法保持
  • 重定向后的HOST错误

本文将帮助大家分析问题原因并给出解决这两个常见问题的方法。

[[190127]]

一、会话保持问题

通过跟踪一个HTTP请求经过Zuul到具体服务,再到返回结果的全过程。我们很容易就能发现,在传递的过程中,HTTP请求头信息中的Cookie和Authorization都没有被正确地传递给具体服务,所以最终导致会话状态没有得到保持的现象。

那么这些信息是在哪里丢失的呢?我们从Zuul进行路由转发的过滤器作为起点,来一探究竟。下面是RibbonRoutingFilter过滤器的实现片段:

  1. public class RibbonRoutingFilter extends ZuulFilter{ 
  2.     ... 
  3.     protected ProxyRequestHelper helper; 
  4.      
  5.     @Override 
  6.     public Object run() { 
  7.         RequestContext context = RequestContext.getCurrentContext(); 
  8.         this.helper.addIgnoredHeaders(); 
  9.         try { 
  10.             RibbonCommandContext commandContext = buildCommandContext(context); 
  11.             ClientHttpResponse response = forward(commandContext); 
  12.             setResponse(response); 
  13.             return response; 
  14.         } 
  15.         ... 
  16.         return null; 
  17.     } 
  18.      
  19.         protected RibbonCommandContext buildCommandContext(RequestContext context) { 
  20.         HttpServletRequest request = context.getRequest(); 
  21.         MultiValueMap<String, String> headers = this.helper 
  22.                 .buildZuulRequestHeaders(request); 
  23.         MultiValueMap<String, String> params = this.helper 
  24.                 .buildZuulRequestQueryParams(request); 
  25.         ... 
  26.     } 

这里有三个重要元素:

  • 过滤器的核心逻辑run函数实现,其中调用了内部函数buildCommandContext来构建上下文内容
  • 而buildCommandContext中调用了helper对象的buildZuulRequestHeaders方法来处理请求头信息
  • helper对象是ProxyRequestHelper类的实例

接下来我们再看看ProxyRequestHelper的实现:

  1. public class ProxyRequestHelper { 
  2.     public MultiValueMap<String, String> buildZuulRequestHeaders( 
  3.             HttpServletRequest request) { 
  4.         RequestContext context = RequestContext.getCurrentContext(); 
  5.         MultiValueMap<String, String> headers = new HttpHeaders(); 
  6.         Enumeration<String> headerNames = request.getHeaderNames(); 
  7.         if (headerNames != null) { 
  8.             while (headerNames.hasMoreElements()) { 
  9.                 String name = headerNames.nextElement(); 
  10.                 if (isIncludedHeader(name)) { 
  11.                     Enumeration<String> values = request.getHeaders(name); 
  12.                     while (values.hasMoreElements()) { 
  13.                         String value = values.nextElement(); 
  14.                         headers.add(name, value); 
  15.                     } 
  16.                 } 
  17.             } 
  18.         } 
  19.         Map<String, String> zuulRequestHeaders = context.getZuulRequestHeaders(); 
  20.         for (String header : zuulRequestHeaders.keySet()) { 
  21.             headers.set(header, zuulRequestHeaders.get(header)); 
  22.         } 
  23.         headers.set(HttpHeaders.ACCEPT_ENCODING, "gzip"); 
  24.         return headers; 
  25.     } 
  26.     public boolean isIncludedHeader(String headerName) { 
  27.         String name = headerName.toLowerCase(); 
  28.         RequestContext ctx = RequestContext.getCurrentContext(); 
  29.         if (ctx.containsKey(IGNORED_HEADERS)) { 
  30.             Object object = ctx.get(IGNORED_HEADERS); 
  31.             if (object instanceof Collection && ((Collection<?>) object).contains(name)) { 
  32.                 return false; 
  33.             } 
  34.         } 
  35.         ... 
  36.     } 

从上述源码中,我们可以看到构建头信息的方法buildZuulRequestHeaders通过isIncludedHeader函数来判断当前请求的各个头信息是否在忽略的头信息清单中,如果是的话就不组织到此次转发的请求中去。那么这些需要忽略的头信息是在哪里初始化的呢?在PRE阶段的PreDecorationFilter过滤器中,我们可以找到答案:

  1. public class PreDecorationFilter extends ZuulFilter{ 
  2.     ... 
  3.     public Object run() { 
  4.         RequestContext ctx = RequestContext.getCurrentContext(); 
  5.         final String requestURI = this.urlPathHelper.getPathWithinApplication(ctx.getRequest()); 
  6.         Route route = this.routeLocator.getMatchingRoute(requestURI); 
  7.         if (route != null) { 
  8.             String location = route.getLocation(); 
  9.             if (location != null) { 
  10.                 ctx.put("requestURI", route.getPath()); 
  11.                 ctx.put("proxy", route.getId()); 
  12.                 // 处理忽略头信息的部分 
  13.                 if (!route.isCustomSensitiveHeaders()) { 
  14.                     this.proxyRequestHelper.addIgnoredHeaders( 
  15.                         this.properties.getSensitiveHeaders() 
  16.                         .toArray(new String[0])); 
  17.                 } else { 
  18.                     this.proxyRequestHelper.addIgnoredHeaders( 
  19.                         route.getSensitiveHeaders() 
  20.                         .toArray(new String[0])); 
  21.                 } 
  22.         ... 

从上述源码中,我们可以看到有一段if/else块,通过调用ProxyRequestHelper的addIgnoredHeaders方法来添加需要忽略的信息到请求上下文中,供后续ROUTE阶段的过滤器使用。这里的if/else块分别用来处理全局设置的敏感头信息和指定路由设置的敏感头信息。而全局的敏感头信息定义于ZuulProperties中:

  1. @Data 
  2. @ConfigurationProperties("zuul") 
  3. public class ZuulProperties{ 
  4.     private Set<String> sensitiveHeaders = new LinkedHashSet<>
  5.             Arrays.asList("Cookie", "Set-Cookie", "Authorization")); 
  6.     ... 

所以解决该问题的思路也很简单,我们只需要通过设置sensitiveHeaders即可,设置方法分为两种:

1. 全局设置:

  • zuul.sensitive-headers=

2. 指定路由设置:

  • zuul.routes..sensitive-headers=
  • zuul.routes..custom-sensitive-headers=true

二、重定向问题

在使用Spring Cloud Zuul对接Web网站的时候,处理完了会话控制问题之后。往往我们还会碰到如下图所示的问题,我们在浏览器中通过Zuul发起了登录请求,该请求会被路由到某WebSite服务,该服务在完成了登录处理之后,会进行重定向到某个主页或欢迎页面。此时,仔细的开发者会发现,在登录完成之后,我们浏览器中URL的HOST部分发生的改变,该地址变成了具体WebSite服务的地址了。这就是在这一节,我们将分析和解决的重定向问题!

Spring Cloud Zuul对接Web网站

出现该问题的根源是Spring Cloud Zuul没有正确的处理HTTP请求头信息中的Host导致。在Brixton版本中,Spring Cloud Zuul的PreDecorationFilter过滤器实现时完全没有考虑这一问题,它更多的定位于REST API的网关。所以如果要在Brixton版本中增加这一特性就相对较为复杂,不过好在Camden版本之后,Spring Cloud Netflix 1.2.x版本的Zuul增强了该功能,我们只需要通过配置属性zuul.add-host-header=true就能让原本有问题的重定向操作得到正确的处理。关于更多Host头信息的处理,读者可以参考本文之前的分析思路,可以通过查看PreDecorationFilter过滤器的源码来详细更多实现细节。

【本文为51CTO专栏作者“翟永超”的原创稿件,转载请通过51CTO联系作者获取授权】

 

戳这里,看该作者更多好文

责任编辑:赵宁宁 来源: 51CTO专栏
相关推荐

2017-05-19 15:13:05

过滤器Spring ClouZuul

2017-05-18 14:14:25

过滤器Spring ClouZuul

2017-07-31 15:47:50

Zuul统一处理

2017-10-20 14:55:06

Spring ClouZuul加载

2017-10-18 16:00:14

SpringCloudZuul路径

2017-08-10 16:14:07

FeignRPC模式

2017-09-26 16:17:39

Ribboneager-load模式

2010-12-14 15:07:15

ICMP路由重定向

2017-04-12 14:43:01

Spring ClouZuul过滤器

2010-07-13 14:10:44

ICMP协议

2017-05-04 22:30:17

Zuul过滤器微服务

2021-04-20 10:15:34

Spring ClouZuul架构

2017-04-13 11:06:28

SpringCloud随机端口

2022-11-10 15:08:44

Linux输入输出

2009-06-25 14:54:22

Servlet转发Servlet重定向

2010-08-17 11:35:00

2010-12-31 13:35:25

文件夹重定向

2020-07-27 07:41:23

Linux重定向数据流

2020-02-24 11:37:56

Linux重定向管道

2009-06-30 15:37:27

Servlet和JSP
点赞
收藏

51CTO技术栈公众号