Java 重定向时向 Zuul 添加标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50040703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding Headers to Zuul when re-directing
提问by Bocky
I am trying to use Zuul
to redirect calls to a downstream system somewhere else.
In the re-direct, I need to add in a Header with necessary data for the api
receiving the redirection to process. I can't seem to get the downstream system to detect this data. Attached is my code.
I am using Zuul
from Edgware.SR3, Spring Boot 1.5.12
我正在尝试Zuul
将调用重定向到其他地方的下游系统。在api
重定向中,我需要添加一个包含必要数据的 Header,以便接收要处理的重定向。我似乎无法让下游系统检测到这些数据。附上我的代码。我正在使用Zuul
从Edgware.SR3, Spring Boot 1.5.12
Zuul Filter
祖尔过滤器
@Component
public class RouteFilter extends ZuulFilter{
@Override
public Object run() {
//Testing to add header
context.getRequest().getParameterMap().put("api", new String[]{"api"});
context.getResponse().setHeader("api", api);
context.addZuulResponseHeader("api", "api");
context.addZuulRequestHeader("api", "api");
context.setSendZuulResponse(false);
context.put(FORWARD_TO_KEY, redirect_urls.get(key));
context.setResponseStatusCode(HttpStatus.SC_TEMPORARY_REDIRECT);
context.getResponse().sendRedirect(redirect_urls.get(key));
return null;
}
}
Redirected Service Code
重定向服务代码
@RequestMapping(value = "/forward")
public ResponseEntity<String> forwardToMe(@RequestHeader(required = true, name = "api")String api){
return new ResponseEntity<String>("Hi",HttpStatus.OK);
}
Error Received in Postman
在邮递员中收到错误
{ "timestamp": 1524737817729, "status": 400, "error": "Bad Request", "exception": "org.springframework.web.bind.ServletRequestBindingException", "message": "Missing request header 'api' for method parameter of type String", "path": "/forward" }
{“时间戳”:1524737817729,“状态”:400,“错误”:“错误请求”,“异常”:“org.springframework.web.bind.ServletRequestBindingException”,“消息”:“缺少请求标头'api' String 类型的方法参数", "path": "/forward" }
回答by redoff
I guess you use a Route Filter, maybe you can try with a Pre Filter.
我猜您使用的是Route Filter,也许您可以尝试使用Pre Filter。
Adding a custom header can be done with something like this : context.addZuulRequestHeader("Authorization", "Basic " + credentials);
.
添加自定义标题可以像这样进行:context.addZuulRequestHeader("Authorization", "Basic " + credentials);
。
For the redirection part, you can check this thread
对于重定向部分,您可以查看此线程
回答by Thomas Tse
I update my comment here just in case if anyone is still facing this problem. I found this problem recently and resolved by adding the following configuration in my application.yml
我在这里更新我的评论,以防万一有人仍然面临这个问题。我最近发现了这个问题并通过在我的 application.yml 中添加以下配置来解决
application.yml
...
zuul:
sensitive-headers:
- Cookie,Set-Cookie
...
application.yml
...
zuul:
sensitive-headers:
- Cookie,Set-Cookie
...
Reference Link below:
https://cloud.spring.io/spring-cloud-static/Dalston.SR5/multi/multi__router_and_filter_zuul.html
参考链接如下:https:
//cloud.spring.io/spring-cloud-static/Dalston.SR5/multi/multi__router_and_filter_zuul.html
回答by Didier R. G.
A little late my response but works fine
As referred in the official documentation Cookies and Sensitive Headers
The sensitiveHeaders are a blacklist, and the default is not empty. Consequently, to make Zuul send all headers (except the ignored ones), you must explicitly set it to the empty list
. Doing so is necessary if you want to pass cookie or authorization headers to your back end. The following example shows how to use sensitiveHeaders:
有点晚了我的反应,但能正常工作
作为官方文件中被称为饼干和敏感头
的sensitiveHeaders是一个黑名单,默认不为空。因此,to make Zuul send all headers (except the ignored ones), you must explicitly set it to the empty list
。如果您想将 cookie 或授权标头传递到您的后端,那么这样做是必要的。以下示例显示了如何使用sensitiveHeaders:
zuul:
routes:
entry:
path: /users/**
strip-prefix: false
service-id: users-service
sensitive-headers:
- Cookie,Set-Cookie
This implemented examplecan also help you
这个实现的例子也可以帮助你