HttpServletRequest getLocalAddr not returning local address after migrating to spring 2.1.6

家住魔仙堡 提交于 2019-12-11 15:29:33

问题


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

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.

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 use tomcat 9.0.19
  • Spring-boot 2.1.6 should use tomcat 9.0.20 according to the release not but when I execute mvn dependency:tree I find that it uses 9.0.21.

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.

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();
}
}

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


回答1:


This was a regression bug in tomcat 9.0.21 which is the version provided by spring boot 2.1.6. There is a fix in 9.0.23. Details here: https://bz.apache.org/bugzilla/show_bug.cgi?id=63570




回答2:


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.

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>

See:

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

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



来源:https://stackoverflow.com/questions/57326478/httpservletrequest-getlocaladdr-not-returning-local-address-after-migrating-to-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!