How Java obtains the true IP address of other users ' logins

Source: Internet
Author: User

To get the IP of other users, you may encounter request.getremoteaddr () to get a value of 0:0:0:0:0:0:0:1, which is why, according to the truth, Should be 127.0.0.1, why the value of this to become a IPv6, and I found that this situation only in the server and the client are on the same computer will appear (for example, when the use of localhost access), and later on the Internet check the reason, the original is/etc/ Hosts this thing mischief (on Windows should be C:\Windows\system32\drivers\etc\ hosts this file), only need to comment out the file #:: 1 localhost This line can solve the problem. In addition, localhost this file is very useful, here you can add your own entries, such as adding 192.168.0.212 myweb this way, in the browser can only use 192.168.0.212来 access, and can use MyWeb to replace.

1  Publicloginnote Loginnoteip (String username, httpservletrequest request) {2         //Get IP3     //inetaddress netaddress = getinetaddress ();4         //your own definition of PO class5Loginnote Loginnote =Newloginnote ();6         //Get IP Address7String IP =request.getlocaladdr ();8         if(IP = =NULL|| Ip.length () = = 0) {9ip = Request.getheader ("X-forwarded-for");Ten             if(IP = =NULL|| Ip.length () = = 0 One|| "Unknown". Equalsignorecase (IP)) { Aip = Request.getheader ("Proxy-client-ip"); -             } -             if(IP = =NULL|| Ip.length () = = 0 the|| "Unknown". Equalsignorecase (IP)) { -ip = Request.getheader ("Wl-proxy-client-ip"); -             } -             if(IP = =NULL|| Ip.length () = = 0 +|| "Unknown". Equalsignorecase (IP)) { -IP =request.getremoteaddr (); +             } A         } at         //since some of the logins are through proxies, take the first one (the first one is really IP) -          intindex = Ip.indexof (', '); -            if(Index! =-1) { -ip = ip.substring (0, index); -         }   - loginnote.setip (IP); inLoginnote.settime (NewDate ()); - Loginnote.setusername (username); to         returnLoginnote; +}
View Code

If you only need to get your own IP, everyone login displays the IP of the server.

1  2InetAddress netaddress =getinetaddress ();3 //call the following code4 5 6  PrivateInetAddress getinetaddress () {Try{return7Inetaddress.getlocalhost (); }Catch(unknownhostexception e) {8SYSTEM.OUT.PRINTLN ("IP address not known"); }return NULL; }9       Ten       Private StaticString Gethostip (inetaddress netaddress) {if(NULL== Onenetaddress) {return NULL; } String IP = netaddress.gethostaddress ();// A* Get IPreturnip }
View Code

How Java obtains the true IP address of other users ' logins

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.