One. Demand
Recently in a module, there is a background statistics function required According to the user's province to be counted.
So when the user is operating, the background should get their location information, and stored in the database.
Two. Analysis
As a mobile app, you want to target users
1. Can be obtained through the client system (Android,ios), (previously Android did use Baidu lbs positioning)
2. Can be H5 through the page, GPS positioning (the foreground calls some interfaces I also do not understand, it seems to be so, it is not the focus anyway)
3. You can use the background IP address, and then find the location through the IP library
As the company is using the H5 page, so the location is given to the H5 and backstage.
Because the front desk is too troublesome, so the positioning of the above-mentioned third method to do.
Although the IP address has been updated, this method does not necessarily keep accurate, but it is the simplest.
Three. Get started.
Just before the company had a project used to locate
Previously used is latitude and longitude +ip, first let the client staring at latitude, if not passed over, then the background through the IP to judge.
Come to me and judge directly by IP.
Get the code used before, see a pass, the original use is 17mon free API, then this aspect does not say much, is according to the document Tune API.
The problem now is to get the IP address.
Four. Get IP
1. Issue 1-Invoke API
Request.getremoteaddr ();
This can really get to IP, but the enterprise application will be a problem, here is not to say, assuming that (see 3).
2. Question 2-Test
The problem comes, get to LAN IP, call 17mon API to get the result is "LAN"
So, the LAN is not testing this (at least I have no good way), so moved to the company's stage (external network) environmental testing
3. Issue 3-Acquired IP is 127.0.0.1
Why. Because the company has Ngnix reverse proxy server, the client's IP address was swallowed by him,
What to do. Let Ngnix spit out:
Request.getheader ("X-forwarded-for")
The string returned here is most likely to have multiple IPs, separated by ",". This multiple IP is a layer of multiple agents, we just get to the first valid.
Five. The problem I encountered here:
The company has written a good API, so I took it directly to use, x-forwarded-for what I did not control, he has.
But I can not get the x-forwarded-for header when I call (repeat with log, deploy, tired to say).
Then think, the client should have access to the server, now in the middle through the agent, usually our access to the server is not x-forwarded-for This parameter, then this parameter, it must be the agent to leave the client's trail add in; So search the internet Ngnix X-forwarded-for, it is true that the Item property is not configured in Ngnix, add on. Ok.
Six. On the code:
Internet access to the IP is this set, took the company's code (as the online roughly the same), should not be hit ...
/** * Get the client IP address * * @param request requests * @return Valid remote address */public static String GetC
Lientip (HttpServletRequest request) {String remoteaddr = Request.getheader ("X-forwarded-for");
Logger.info ("remoteaddr:" + remoteaddr); If you pass a multilevel reverse proxy, the value of x-forwarded-for is more than one, but a comma-delimited IP value, which takes the first non-unknown valid IP string in x-forwarded-for if (Iseffective (remotead
DR) && (Remoteaddr.indexof (",") >-1)) {string[] array = Remoteaddr.split (",");
for (String Element:array) {if (iseffective (Element)) {remoteaddr = element;
Break
}}} if (!iseffective (remoteaddr)) {remoteaddr = Request.getheader ("X-real-ip");
Logger.info ("Request.getheader (\" X-real-ip\ "):" + remoteaddr);
} if (!iseffective (remoteaddr)) {remoteaddr = Request.getremoteaddr (); LOgger.info ("Request.getremoteaddr ():" + remoteaddr);
} return remoteaddr;
}/** * The remote address is valid. * * @param remoteaddr Remote address * @return True means the remote address is valid, false means the remote address is invalid */private static Boolean iseffective (f
Inal String remoteaddr) {Boolean iseffective = false; if ((null! = REMOTEADDR) && (! "". Equals (Remoteaddr.trim ())) && (! "
Unknown ". Equalsignorecase (Remoteaddr.trim ()))) {iseffective = true;
} return iseffective; }