Adding Headers to Zuul when re-directing

ぃ、小莉子 提交于 2020-01-11 08:48:14

问题


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 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" }


回答1:


I guess you use a Route Filter, maybe you can try with a Pre Filter.

Adding a custom header can be done with something like this : context.addZuulRequestHeader("Authorization", "Basic " + credentials);.

For the redirection part, you can check this thread




回答2:


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 ... 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://stackoverflow.com/questions/50040703/adding-headers-to-zuul-when-re-directing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!