Java: HttpServletRequest.getRemoteAddr() not shows IP same as CURL or IFConfig

168 Views Asked by At

My QA Server IFConfig is showing following IP:

docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.16.0.1  netmask 255.255.0.0  broadcast 0.0.0.0
        ether 02:42:73:6e:ba:54  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eno16780032: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 146.213.179.50  netmask 255.255.255.210  broadcast 146.213.179.119
        inet6 fc80::250:56ff:fea0:6dc9  prefixlen 64  scopeid 0x20<link>
        ether 00:50:56:a0:6d:b9  txqueuelen 1000  (Ethernet)
        RX packets 529734790  bytes 285217050121 (265.6 GiB)
        RX errors 0  dropped 19  overruns 0  frame 0
        TX packets 502519153  bytes 423616456297 (394.5 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eno33559296: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.1.2  netmask 255.255.255.240  broadcast 10.0.1.14
        inet6 fc80::250:56ff:fea0:498c  prefixlen 64  scopeid 0x20<link>
        ether 00:50:56:a0:49:8b  txqueuelen 1000  (Ethernet)
        RX packets 176687369  bytes 54833457460 (51.0 GiB)
        RX errors 0  dropped 14  overruns 0  frame 0
        TX packets 180706064  bytes 34257419515 (31.9 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eno50338560: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.2.8  netmask 255.255.255.240  broadcast 10.0.2.10
        inet6 fc80::250:56ff:fca0:67c7  prefixlen 64  scopeid 0x20<link>
        ether 00:50:56:a0:67:a7  txqueuelen 1000  (Ethernet)
        RX packets 3322039516  bytes 2382840054404 (2.1 TiB)
        RX errors 0  dropped 20  overruns 0  frame 0
        TX packets 2212620398  bytes 2317370292546 (2.1 TiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 130568922  bytes 39557947765 (36.8 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 130568922  bytes 39557947765 (36.8 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

When i use curl command on QA; it shows following:

curl bot.whatismyipaddress.com
153.110.231.215

But when i write following java Rest API on same Machine (wildfly server) and call that API from another client in same machine (wildfly server) using public domain name. I get following IP:

146.192.61.46

Code:

@GetMapping("/customers/all")
public List<XxtoneCustomersV> getAllCustomers(HttpServletRequest request){
    LOG.info("Remote Addr: " + request.getRemoteAddr());

When i hit same rest api from local i get my correct IP Address.

Why is such difference in IP in linux commands and java code? Am i using correct java method?

Also the one i retrieved from Curl appears Public IP (if ifconfig returns internal ones) so same should not get in remoteAddr?

1

There are 1 best solutions below

0
Kemal Kaplan On

This is probably because the second client in the QA machine routes the request through local interface instead of the Internet. To overcome this, first check which IP the second client resolves for the public domain name. If it resolves an internal IP, then you may add the public IP to the host file to force the second client to use Internet route.