一,忽略请求头信息

application.yml文件中添加需要过滤头信息的名称:

1
2
zuul:
ignoredHeaders: Access-Control-Allow-Credentials, Access-Control-Allow-Origin, Cookie,Set-Cookie

这样如果头信息存在这些开头的时候就会自动被过滤掉。

二,添加请求头信息

添加头信息,需要获取RequestContext:

1
2
RequestContext context = RequestContext.getCurrentContext();
context.addZuulRequestHeader("Authorization", "Bearer " + "xxx");

三,移除请求头信息(代码)

修改请求头信息及对存在请求的头信息进行修改,首先,通过RequestContext获取到请求,在对整个请求做一次包装,最后在设置到RequestContext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
RequestContext context = RequestContext.getCurrentContext();
HttpServletRequest request = context.getRequest();

request = new HttpServletRequestWrapper(request) {
private Set < String > headerNameSet;

@Override
public Enumeration < String > getHeaderNames() {
if (headerNameSet == null) {
// first time this method is called, cache the wrapped request's header names:
headerNameSet = new HashSet < > ();
Enumeration < String > wrappedHeaderNames = super.getHeaderNames();
while (wrappedHeaderNames.hasMoreElements()) {
String headerName = wrappedHeaderNames.nextElement();
if (!"Authorization".equalsIgnoreCase(headerName)) {
headerNameSet.add(headerName);
}
}
}
return Collections.enumeration(headerNameSet);
}
};

context.setRequest(request);

同时也可以对需要的请求信息做出修改。