zuul过滤器中怎么转发请求头

这期内容当中小编将会给大家带来有关zuul过滤器中怎么转发请求头,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

zuul过滤器中转发请求头

Zuul网关过滤的时候,如果不在网关过滤时转发请求头,经过网关时请求头就会丢失。

网关过滤转发请求头代码如下

@Override
    public Object run() throws ZuulException {
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        Enumeration<String> headerNames = request.getHeaderNames();
        if (headerNames != null) {
            while (headerNames.hasMoreElements()) {
                String name = headerNames.nextElement();
                String values = request.getHeader(name);
                currentContext.addZuulRequestHeader(name, values);
            }
        }
        return null;
    }

但我用 addZuulRequestHeader转发请求头却不起作用。原来是敏感请求头Authorization,Cookie,Set-Cookie默认不转发的。

解决方法一:使用别的单词,如 token、myAuthorizaion等

解决方法二:配置application.yml

zuul:
  routes:
    api-seller:
      path: /api/seller/**
      serviceId: mobileshop-api-seller-feign
  sensitive-headers: 
    - Cookie,Set-Cookie,Authorization

zuul路由转发header丢失问题

zuul过滤器中怎么转发请求头

定义一个前置过滤器,获取原生request的请求头,塞到zuul转发request请求头中。

上述就是小编为大家分享的zuul过滤器中怎么转发请求头了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注行业资讯频道。