【发布时间】:2016-05-28 23:51:37
【问题描述】:
在过去的 2 天里,我一直试图找出一个奇怪的重定向问题,但没有成功。
基于spring-cloud示例项目,我已经配置了Eureka、Zuul和一个运行在Zuul后面的基础服务。
我有以下方法;
@RequestMapping(method = RequestMethod.POST, value = "/register")
public String registerDevice(Principal principal, String response) {
// ...
return "redirect:/account";
}
表单设置为发布到代理 URL,如下所示;
POST https://localhost:8443/service/register
(Zuul 在 localhost:8443 上运行)。
本地服务(非代理)的 URL 将是; http://localhost:9001/register
通过上述方法正确代理了POST调用,但是发送到浏览器的重定向位置是服务的非代理URL; http://localhost:9001/account
Zuul 代理肯定会发送正确的 x-forwarded-* 标头,因此我希望 Spring 中的视图解析器能够基于 x-forwarded 值构建正确的重定向。
为了证明标头发送正确,我重新配置了方法如下;
@RequestMapping(method = RequestMethod.POST, value = "/register")
public void registerDevice(Principal, String response, HttpServletResponse response) {
// ...
String rUrl = ServletUriComponentsBuilder.fromCurrentContextPath().path("/account").build().toUriString();
servletResponse.sendRedirect(rUrl);
}
正确地将浏览器重定向到代理位置; https://localhost:8443/service/account
这是一个错误,还是预期的行为?我认为使用“重定向:”是为了尊重从代理传递的转发标头。
【问题讨论】:
-
你找到解决方案了吗?
标签: spring-mvc spring-boot spring-cloud netflix-eureka netflix-zuul