1

i'm trying to retrieve ip address from HttpServletRequest object using java (Spring mvc).
Few of my systems are connected in lan with different ip address. i'm able to host my web app (using apache tomcat 7) in one system and access the web app in other system's. i tried using below code to retrieve the IP address from the http request header by triggering url from browser from different systems.
but i can see only the value 127.0.0.1 or localhost:8080

request.getHeader("X-FORWARDED-FOR");  // returns null
request.getRemoteAddr(); // returns 127.0.0.1

Enumeration headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        String key = (String) headerNames.nextElement();
        String value = request.getHeader(key);
        logger.info("1key: "+key+" 1value: "+value);
    }
//(along with other values, i get) ip value as localhost:8080 


i think the ip shown by the above code, is the ip of the system where the app is launched.

Lets say there are 5 systems
system 1 - ip: 172.22.16.1 (HOST system)
system 2 - ip: 172.22.16.2
system 3 - ip: 172.22.16.3
system 4 - ip: 172.22.16.4
system 5 - ip: 172.22.16.5

when i access the web app from system 5, i need to capture the ip address of system 5 which is 172.22.16.5. But in my case i get only 127.0.0.1

Please let me know what is the problem here and why i'm not able to see the IP address from which request is originating and what should i do to get the output i require.

Update
Though i didnot solve my underlying problem, i was able to figure out what was causing the problem (browse through comments). The discussion helped

divine
  • 4,746
  • 3
  • 27
  • 38
  • 2
    Then you are calling the system from the same system. If you are on system5 and call system5 it will be localhost. – M. Deinum Feb 04 '16 at 12:53
  • @M.Deinum sorry i can't understand your text. my problem is exactly what i mentioned in post. i have hosted the app on system 1 and i'm accessing the app from system 5. could you tell me what mistake i'm making here? – divine Feb 04 '16 at 13:01
  • 1
    Then there is a proxy in on system 1. [`getRemoteAddr`](https://docs.oracle.com/javaee/5/api/javax/servlet/ServletRequest.html#getRemoteAddr()) will return the remote address of the client or last proxy (as specified by the javadoc). So I guess the issue is with what lies before your host. – M. Deinum Feb 04 '16 at 13:04
  • question.. why do you need your IPs ? HTTP is built on top of TCP/IP in order to simplify everything, why go back ? :) – sashok_bg Feb 04 '16 at 13:13
  • @M.Deinum how to find out that my system has proxy or not?. as i'm connecting to local system, i think proxy wouldn't play a role here? as a whole my office has proxy for connecting to internet. i have disabled proxy settings in browser in all 5 systems. i can access the app from all 5 systems. but still i get 127.0.0.1 – divine Feb 04 '16 at 13:14
  • @sashok_bg i need ip's to implement a login/registration functionality , so that an user with his email can only access the app from that particular system and not from any other system's. its for internal purpose, the scope of usage is only within LAN and not available in internet. please provide solution – divine Feb 04 '16 at 13:16
  • @M.Deinum open vpn is running in my system. you think that will cause this confusion? – divine Feb 04 '16 at 13:33
  • This behavior should be enforced by server rewrite rules, proxies and other network tools and not by your application – sashok_bg Feb 04 '16 at 13:46
  • I think X-Forwarded-For should be the header that you must be looking for. – Rohith K Feb 04 '16 at 14:07
  • @Jango it returns nothing, as i mentioned in the post – divine Feb 04 '16 at 14:25
  • @sashok_bg good point. any ideas to move forward from my present state is appreciated – divine Feb 04 '16 at 14:26
  • As I dig into this a bit I'm not sure I understand what you are trying to determine. A machine can have a variety of valid ip addresses. Can you explain what you are trying to solve? – JohnCampbellJr Feb 04 '16 at 15:11
  • @JohnCampbellJr read my post . i cant explain more that.sorry – divine Feb 04 '16 at 15:29
  • 1
    @divine the issue here is that there will (usually) be multiple ip addresses for a machine. I suspect you want the ip address for a purpose--for example, to distinguish which machine serviced a request. – JohnCampbellJr Feb 04 '16 at 15:36
  • 1
    @JohnCampbellJr the web app should only be accessible on LAN network. IMO the question should be reformulated and probably assigned to network / server config category – sashok_bg Feb 04 '16 at 15:37
  • @JohnCampbellJr yes. you are right. i use openvpn to connect to another system and my app needs to connect to that system.so multiple ip exists. i was suspecting this. i just created an independent test app and now it works fine on getting ip address from request header....lol... – divine Feb 04 '16 at 15:40
  • @sashok_bg its ok. thank you for your help. its working right now. open vpn was the issue. i have briefed about it in my above comment – divine Feb 04 '16 at 15:42
  • @sashok_bg i saw your answer. it works for me and i explained here how...My system and myapp is in System1. my app somehow needs few information from System2. We use open vpn to connect to System2, which assigns an additional ip address to my System1 so that my app can fetch certain information. in this scenario, i'm trying to access the app in system1 from other system's connected in lan and thats why ip in http request messed up, i guess. – divine Feb 04 '16 at 15:50

2 Answers2

1

If your final goal is to restrict the access of your application only to a given network, then you should achieve this by server and network configuration. One such example is to configure an apache HTTPD server with a virtual host, restricting access only to a given network:

http://www.cyberciti.biz/faq/apache-restrict-access-based-on-ip-address-to-selected-directories/

Open your httpd.conf file:

vi /etc/httpd/conf/httpd.conf Locate directory section (for example/var/www/sub/payroll) and set it as follows:

Order allow,deny Allow from 192.168.1.0/24 Allow from 127

Alternatively you can configure your tomcat to server only on your "local" IP, since I presume you are on a NAT and your server's IP is something like 192.168.1.11.

How do you configure tomcat to bind to a single ip address (localhost) instead of all addresses?

Also, your application should be by default only visible to your local network and you should normally do some extra configuration and routing to make it visible for the outside users.

Community
  • 1
  • 1
sashok_bg
  • 2,436
  • 1
  • 22
  • 33
0

One solution would be to add HttpServletRequest request to your method and then use the Servlet API as follows:

System.out.println(request.getRemoteAddr());
Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
Alex
  • 16
  • 3
  • i use HttpServletRequest object in my program. but i'm not getting the result i'm expecting as i mentioned in my post. – divine Feb 04 '16 at 12:58