简体   繁体   English

如何使用Java开发的Web应用程序从http请求中检索源系统的ip地址?

[英]How to retrieve ip address of the source system from http request from web app developed with java?

i'm trying to retrieve ip address from HttpServletRequest object using java (Spring mvc) . 我正在尝试使用java(Spring mvc)从HttpServletRequest对象检索IP地址。
Few of my systems are connected in lan with different ip address. 我的系统中很少有人用不同的IP地址在局域网中连接的。 i'm able to host my web app (using apache tomcat 7) in one system and access the web app in other system's. 我能够在一个系统中托管我的Web应用程序(使用apache tomcat 7) ,并在其他系统中访问该Web应用程序。 i tried using below code to retrieve the IP address from the http request header by triggering url from browser from different systems. 我尝试使用以下代码通过从不同系统的浏览器触发url从http请求标头中检索IP地址。
but i can see only the value 127.0.0.1 or localhost:8080 但我只能看到值127.0.0.1或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. 我认为以上代码显示的ip是启动应用程序的系统的ip。

Lets say there are 5 systems 可以说有5个系统
system 1 - ip: 172.22.16.1 (HOST system) 系统1-ip:172.22.16.1(主机系统)
system 2 - ip: 172.22.16.2 系统2-ip:172.22.16.2
system 3 - ip: 172.22.16.3 系统3-ip:172.22.16.3
system 4 - ip: 172.22.16.4 系统4-ip:172.22.16.4
system 5 - ip: 172.22.16.5 系统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. 当我从系统5访问Web应用程序时,我需要捕获系统5的IP地址172.22.16.5。 But in my case i get only 127.0.0.1 但就我而言,我只能得到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. 请让我知道这里的问题是什么,为什么我看不到发出请求的IP地址,以及我应该怎么做才能获得所需的输出。

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 讨论有帮助

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: 这样的示例之一是使用虚拟主机配置apache HTTPD服务器,从而仅限制对给定网络的访问:

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

Open your httpd.conf file: 打开您的httpd.conf文件:

vi /etc/httpd/conf/httpd.conf Locate directory section (for example/var/www/sub/payroll) and set it as follows: vi /etc/httpd/conf/httpd.conf找到目录部分(例如/ var / www / sub / payroll)并进行如下设置:

Order allow,deny Allow from 192.168.1.0/24 Allow from 127 订购允许,拒绝允许自192.168.1.0/24允许自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. 另外,您可以将tomcat配置为仅在“本地” IP上使用服务器,因为我假设您在NAT上,并且服务器的IP类似于192.168.1.11。

How do you configure tomcat to bind to a single ip address (localhost) instead of all addresses? 如何配置tomcat绑定到单个IP地址(本地主机)而不是所有地址?

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. 另外,默认情况下,您的应用程序应仅对本地网络可见,并且通常应进行一些额外的配置和路由,以使其对外部用户可见。

一种解决方案是将HttpServletRequest请求添加到您的方法,然后按如下所示使用Servlet API:

System.out.println(request.getRemoteAddr());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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