ServletUriComponentsBuilder.fromRequest 返回 http 方案而不是 https

Nik*_*Nik 5 spring-mvc

我有一个由前端代码组成的 Web 应用程序,它调用由两个后端模块实现的相同 api。这个 api 返回一个 JSON 对象中的 url。后端模块都是用 spring mvc 编写的,但版本不同。url-building 是一样的,它是这样的:

@GetMapping(path = "/app1/menu", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
   public JsonObject getMenu(HttpServletRequest req) throws IOException {
       JsonObject menu = new JsonObject();
       menu.addProperty("href", ServletUriComponentsBuilder.fromRequest(req)
               .replacePath(req.getContextPath())
               .path("/index.html")
               .toUriString());
       return menu;
}
Run Code Online (Sandbox Code Playgroud)

如您所见,此代码只是向传入的请求添加一个常量并返回它。第一个应用程序使用 spring mvc 4(准确地说是 4.3.5.RELEASE)。第二个模块使用 5.1.4.RELEASE 版本。当所有这些应用程序都部署在负载均衡服务器(2 个带有负载均衡器的 tomcat 实例)和 https 上时,问题就出现了。假设请求 url 对于 app1 是这样的:

https://example.com/context/app1/menu
Run Code Online (Sandbox Code Playgroud)

app1 正确返回

https://example.com/context/index.html
Run Code Online (Sandbox Code Playgroud)

对于 app2,前端发出的请求是 https://example.com/context/app2/menu

答案是 http://example.com/context/another_index.html

所以它失去了 https 方案

似乎 ServletUriComponentsBuilder.fromRequest 改变了行为?我已经(我承认很快)查看了 git repo 中的提交,但没有找到任何东西......