在过去的两天里,我一直在试图弄清一个奇怪的重定向问题,但没有成功。
基于spring云示例项目,我配置了Eureka、Zuul和一个运行在Zuul后面的基本服务。
我有以下方法;
@RequestMapping(method = RequestMethod.POST, value = "/register")
public String registerDevice(Principal principal, String response) {
// ...
return "redirect:/account";
}该表单设置为post到代理URL,如下所示;
POST https://localhost:8443/service/register(Zuul在本地主机上运行:8443)。
本地服务(非代理)的URL为;http://localhost:9001/register。
将POST调用正确地通过上述方法进行代理,但是发送到浏览器的重定向位置是服务的非代理URL;http://localhost:9001/account。
Zuul代理肯定发送了正确的x转发-*头,因此我希望Spring中的视图解析器能够基于x转发的值构建正确的重定向。
为了证明报头发送正确,我重新配置了如下方法;
@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
这是一个错误,还是预期的行为?我认为使用“重定向:”是为了尊重从代理传递的前向标头。
发布于 2016-02-17 17:02:49
如您所见,RedirectView会忽略X-FORWARDED-*头。简单地说,您不能使用"redirect:/account"“。
相反,实例化一个RedirectView并相应地配置它:
RedirectView redirect = new RedirectView("account");
redirect.setHosts(new String[]{ request.getHeader("X-FORWARDED-HOST") });因为SpringFramework4.3(目前是RC1)方法是可用的。
发布于 2017-04-18 08:06:41
如果在后端应用程序中使用tomcat作为嵌入式服务器,则可以使用此设置(application.properties、yml等):
server.tomcat.remote_ip_header=x-forwarded-for
server.tomcat.protocol_header=x-forwarded-proto或者更一般的方式:
server.use-forward-headers=truehttps://stackoverflow.com/questions/35453939
复制相似问题