Get User (operator) Real IP

Source: Internet
Author: User

1: Get local IP (locally)

If the application is deployed to the server, the IP acquired is the server address

private static String Getwebserverip () {

StringBuilder ipstring = new StringBuilder ();

Enumeration<networkinterface> netinterfaces = null;

inetaddress IP = null;

try {

Netinterfaces = Networkinterface.getnetworkinterfaces ();

while (Netinterfaces.hasmoreelements ()) {

NetworkInterface ni = netinterfaces.nextelement ();

enumeration<inetaddress> ips = ni.getinetaddresses ();

while (Ips.hasmoreelements ()) {

ip = ips.nextelement ();

if (IP! = null &&!ip.isloopbackaddress ()) {

if (ip.gethostaddress () = null && ip.gethostaddress (). IndexOf (":") < 0) {

Ipstring.append (Ip.gethostaddress ()). Append ("");

}

}

}

}

} catch (Exception e) {

Log.error ("Get Local IP Error", e);

}

return ipstring.tostring ();

}

2: Get the user's real IP

public class Getipaction extends Baseaction {

Public String getremoteipnew () {

Result result = new result ();

if (request = = null) {

return null;

}

/** * Source 1 for use with reverse proxy operating environment, such as production, testing

* Source 2 for the native development environment

*/

String IP = null;

Source 1 priority fetching IP from X-forwarded-for

String ipold = Request.getheader ("J-forwarded-for");

System.out.println ("Ipwebutils->getremoteipnew j-forwarded-for:" + ipold);

if (Ipold = = NULL | | ipold.length () = = 0 | | "Unknown". Equalsignorecase (Ipold)) {

Ipold = Request.getheader ("X-forwarded-for");

System.out.println ("Ipwebutils->getremoteipnew x-forwarded-for:" + ipold);

}

if (Stringutils.isblank (ipold) | | "Unknown". Equalsignorecase (Ipold)) {

Source 2, development environment use

Ipold = Request.getremoteaddr ();

System.out.println ("Ipwebutils->getremoteipnew getremoteaddr:" + ipold);

}

if (Ipold.contains (",")) {

Use of proxies, such as 12.113.55.66,15.23.41.52

String proxyip[] = Ipold.split (",");

1. Using the agent's normal condition, the XFF structure is [user Ip,proxy1,proxy2,... Proxyn,loadbalanceip]

Possible cases of 2.xff forgery [Proxy1,proxy2,... Proxyn, user Ip,loadbalanceip]

3. Grab the first extranet IP as a user IP from behind

4. To prevent xff forgery attacks, it may result in authentication codes for some users who use proxies

for (int i = proxyip.length-1; I >= 0; i--) {

String tempip = Proxyip[i];

if (!inlan (TEMPIP)) {

First non-LAN IP as User IP

ip = tempip;

Break

}

}

if (IP = = null) {

All IP is intranet IP take the first intranet IP as User IP

ip = proxyip[0];

}

} else {

ip = ipold;

}

ip = Ip.trim ();

Some perverted situation with a port

Result.adddefaultmodel ("message", IP);

Return Ip.contains (":")? Ip.split (":") [0]: IP;

TOVM (result);

return SUCCESS;

}

The function distinguishes different kinds of network models by analyzing the different kinds of IP strings (obtained through HTTP request headers).

The following two methods are used in the above functions.

1: Determine which type of IP format to obtain the address for.

private static Boolean Inlan (String tempid) {

Tempid = Stringutils.trimtoempty (tempid);

int[] INTs = Divideip (tempid); Illegal IP

if (Ints.length < 4) {

return true;

}

if (ints[0] = =) {//a class IP

return true;

}

if (ints[0] = = 192 && ints[1] = = 168 && ints[2] >= 0 && ints[2] <= 255) {

Class C IP

return true; }

if (ints[0] = = 172 && ints[1] >= && ints[1] <= 31) {

Class B IP

return true; }

if ("127.0.0.1". Equals (Tempid)) {

Native Address

return true;

}

return false;

}

2. Split the IP string to get to

private static int[] Divideip (String tempid) {

string[] STRs = tempid.split ("\ \");

Int[] result = new Int[strs.length];

for (int i = 0; i < strs.length; i++) {

try {

Result[i] = Integer.parseint (Stringutils.trimtoempty (strs[i]));

} catch (Exception e) {

}

}

return result;

}

When this method is deployed to a test environment, the IP address obtained is the same address. Due to the inability to actually verify, the IP address that is suspected to be acquired is intranet access to the IP of the external test environment server. Access to the test environment requires address translation, and all IP access to the test environment server is replaced with the same IP. This issue does not exist after the code is put on line.

The X-forward-for IP address obtained after online is the real IP address of the user, and the two proxy addresses are NULL,REMOTE-IP 127.0.0.1.

The reason why this is not very clear, welcome the Great God to guide the discussion.

Get User (operator) Real IP

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.