迁移到 spring 2.1.6 后,HttpServletRequest getLocalAddr 不返回本地地址 [英] HttpServletRequest getLocalAddr not returning local address after migrating to spring 2.1.6

查看:59
本文介绍了迁移到 spring 2.1.6 后,HttpServletRequest getLocalAddr 不返回本地地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近将 spring boot 从 2.1.0 升级到 2.1.6,现在 HttpServletRequest.getLocalAddr() 不再按预期运行.

I've recently upgrading spring boot from 2.1.0 to 2.1.6 and now HttpServletRequest.getLocalAddr() does not behave as expected anymore.

对于上下文,我有一个 Web 客户端和一个 Spring 服务器.当客户端连接到服务器时,后者将其 IP 地址发送给前者.

For context, I have a web client and a spring server. When the client connects to the server, the latter sends its IP address to the former.

在 2.1.0 和 2.1.5 之前,它按预期工作:getLocalAddr 返回 spring 服务器的本地地址.但是,在 2.1.6 中,它现在返回客户端的本地地址.

In 2.1.0 and up to 2.1.5 it worked as expected: getLocalAddr returns the local address of the spring server. However, in 2.1.6 it now returns the local address of the client.

  • Spring-boot 2.1.5 使用 tomcat 9.0.19
  • Spring-boot 2.1.6 应该根据发行版使用 tomcat 9.0.20,但当我执行 mvn dependency:tree 时,我发现它使用的是 9.0.21.

我想知道我是否正确使用了 getLocalAddr 以及最终是否应该将问题提交给 spring-boot 或 tomcat.

I'd like to know if I'm using getLocalAddr properly or not and eventually if I should raise the issue to spring-boot or tomcat.

我已经确认问题发生在 spring 2.1.6 和 tomcat 9.0.21.该问题在 spring 2.1.5 和 tomcat 9.0.19 中不会发生

I've confirmed that the issue occurs in spring 2.1.6 with tomcat 9.0.21. The issue does not occur in spring 2.1.5 with tomcat 9.0.19

我无法提供一个最小的工作示例,但这里有一些代码演示了我的用法.

I'm not able to provide a minimal working example, but here is a bit of code demonstrating my usage.

@RestController
public class DeviceControllerImpl {

//omitting 

@GetMapping("/configuration")
  public String sendConfig(HttpServletRequest http_request) {
    String ip = http_request.getLocalAddr();
}
}

我期望 getLocalAddr 返回 spring 服务器的本地 IP 地址,而不是 Web 客户端.

I'm expecting getLocalAddr to return the local IP address of spring server, not the web client.

推荐答案

这是 Tomcat 9.0.22 版中的一个回归,但在 9.0.23 版中得到修复.但是请注意,9.0.23 版不在 mvnrepository.com 上,所以我会选择 9.0.24 版.

It was a regression in Tomcat version 9.0.22, but was fixed in version 9.0.23. Note however that version 9.0.23 is not on mvnrepository.com, so I would go for version 9.0.24.

要为 Spring Boot 修复它(并假设将 spring-boot-starter-parent 指定为项目的父项),只需在 POM 的属性中指定新版本的 Tomcat:

To fix it for Spring Boot (and assuming the spring-boot-starter-parent is specified as your project's parent) just specify the new version of Tomcat in your POM's properties:

<tomcat.version>9.0.24</tomcat.version>

见:

https://bz.apache.org/bugzilla/show_bug.cgi?id=63570

https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-core

这篇关于迁移到 spring 2.1.6 后,HttpServletRequest getLocalAddr 不返回本地地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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