小编Jam*_*ieB的帖子

在Zuul代理后面的Spring重定向url问题

在过去的两天里,我一直试图找到一个奇怪的重定向问题,但没有成功.

基于spring-cloud示例项目,我已经配置了Eureka,Zuul以及在Zuul后面运行的基本服务.

我有以下方法;

@RequestMapping(method = RequestMethod.POST, value = "/register")
public String registerDevice(Principal principal, String response) {
  // ...
  return "redirect:/account";
}
Run Code Online (Sandbox Code Playgroud)

表单设置为发布到代理URL,如下所示;

POST https://localhost:8443/service/register
Run Code Online (Sandbox Code Playgroud)

(Zuul在localhost上运行:8443).

本地服务的URL(非代理)将是; HTTP://本地主机:9001 /寄存器

POST调用正确代理到上述方法,但是发送到浏览器的重定向位置是服务的非代理URL; HTTP://本地主机:9001 /帐户

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);
}
Run Code Online (Sandbox Code Playgroud)

这正确地将浏览器重定向到代理位置; https://开头本地主机:8443 /服务/帐户

这是一个错误,还是预期的行为?我认为使用"redirect:"是为了纪念从代理传递的前向头.

spring-mvc spring-boot spring-cloud netflix-eureka netflix-zuul

10
推荐指数
1
解决办法
5622
查看次数