简体   繁体   中英

Getting Client ip address from request in WebSphere

I have a scenario where i need to retrieve the client's ipaddress from the request

The code that I am using to get the client's ipaddress is:

String ipAddress = request.getHeader("X-FORWARDED-FOR");
if (ipAddress == null) {
                    ipAddress = request.getRemoteAddr();
                }

As per my understanding, request.getHeader("X-FORWARDED-FOR") will return the client's ipaddress if there is a proxy in between. If there is no proxy in between client and app server, request.getHeader("X-FORWARDED-FOR") will return null but request.getRemoteAddr() can return the ipaddress in that case.

For my local development, I do not have any proxy or loadbalancer in between. So, in the above code, request.getHeader("X-FORWARDED-FOR") will return me null and then I can get the ipaddress from request.getRemoteAddr(); which seems to be working alright.

For my qa and prod environments, we have an F5 LOAD BALANCER which routes the request to IHS Webserver which would then route the request to Websphere appserver on which this code would be deployed.

so, in this distributed environment, iam guessing request.getHeader("X-FORWARDED-FOR") should return me the client's ipaddress correctly. I sis not have a chance to test this in a distributed environment yet.

Is my thinking correct or are there any issues with this approach ?

Advance thanks.

Is my thinking right

Your thinking is mostly correct, but these things are worth noting:

  1. The header is actually X-Forwarded-For (note the case).

  2. You can have more than one such header .

  3. The header itself can contain more than one IP address.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM