Zuul代理背后的Spring重定向URL问题 [英] Spring redirect url issue when behind Zuul proxy

查看:770
本文介绍了Zuul代理背后的Spring重定向URL问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去2天,我一直在尝试解决一个奇怪的重定向问题,但没有成功.

I've been trying to get to the bottom of a strange redirection issue for the past 2 days without success.

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

Based on the spring-cloud example projects, I've configured Eureka, Zuul and a basic service that runs behind Zuul.

我有以下方法;

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

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

The form is setup to post to the proxied URL as follows;

POST https://localhost:8443/service/register

(Zuul在localhost:8443上运行).

(Zuul is running on localhost:8443).

本地服务的URL(未代理)为; http://localhost:9001/register

The URL for the local service (non-proxied) would be; http://localhost:9001/register

通过上述方法可以正确地代理POST调用,但是发送到浏览器的重定向位置是服务的未代理URL; http://localhost:9001/account

The POST call is proxied correctly through to the above method, however the redirect location sent to the browser is the non-proxied URL of the service; http://localhost:9001/account

Zuul代理肯定会发送正确的 x-forwarded-* 标头,因此我希望Spring中的视图解析器会基于x-forwarded值构建正确的重定向.

The Zuul proxy is definitely sending the correct x-forwarded-* headers, so I would expect the view resolver in Spring to build the correct redirect based on the x-forwarded values.

为证明报头已正确发送,我重新配置了以下方法;

To prove the headers are sent correctly, I reconfigured the method as follows;

@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

Which correctly redirects the browser to the proxied location; https://localhost:8443/service/account

这是一个错误,还是预期的行为?我以为使用重定向:"是为了兑现从代理传递过来的转发标头.

Is this a bug, or is it expected behaviour? I thought using "redirect:" was meant to honour the forward headers passed from a proxy.

推荐答案

您可以看到redirect:/account".

As you can see RedirectView ignores X-FORWARDED-* headers. Simply put, you can't use "redirect:/account".

代替实例化RedirectView并进行相应配置:

Instead instantiate a RedirectView and configure it accordingly:

RedirectView redirect = new RedirectView("account");
redirect.setHosts(new String[]{ request.getHeader("X-FORWARDED-HOST") });

从Spring Framework 4.3开始(当前为RC1)

Since Spring Framework 4.3 (currently RC1) setHosts method is available.

这篇关于Zuul代理背后的Spring重定向URL问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆