0

I want to know the location of the user that is on our web application. The information should contain user's Country and City. I am unable to find a good way to do so using Java or Spring Boot. I have tried using GeoLite2-City but it gives me exception saying that The address 10.212.134.200 is not in the database. I was also getting this exception on my localhost with ip 127.0.0.1. Following is the code I have used:

// String ipAddress = httpServletRequest.getRemoteAddr();
public String getGeoLocation(String ipAddress) {
        String geoLocation = "";
        try {
            File database = ResourceUtils.getFile("classpath:GeoLite2-City/GeoLite2-City.mmdb");;
            DatabaseReader dbReader = new DatabaseReader.Builder(database).build();
            CityResponse response = dbReader.city(InetAddress.getByName(ipAddress));
            
            String countryName = response.getCountry().getName();
            String cityName = response.getCity().getName();
            String postal = response.getPostal().getCode();
            String state = response.getLeastSpecificSubdivision().getName();
            
            geoLocation = "Country: " + countryName + "cityName: " + cityName + "postal: " + postal + "state: " + state;
            
            System.out.println(geoLocation);
        } catch(Exception ex) {
            logger.error("Exception occured while trying to get GeoLocation of user: " + ex);
        }
        return geoLocation;
    }

But I am always getting exception.

I only have downloaded and added Geolite2-city file. I am also not confirmed if I need to add any other file. The dependency I have added is:

<dependency>
    <groupId>com.maxmind.geoip2</groupId>
    <artifactId>geoip2</artifactId>
    <version>2.8.0</version>
</dependency>

Note:

GeoLite2 was one of the way I adopted to get the GeoLocation of the user. If you have a better API or suggestion, please suggest me a better way to get user current GeoLocation.
Eatsam ul haq
  • 317
  • 1
  • 12
  • 2
    The 10.x and 127.x are internal IP ranges, so those won't be available, you need external ip-addresses. – M. Deinum Sep 01 '22 at 11:35
  • @M.Deinum So how can I test my code. I deployed the application on server and then tried to access from my machine. In this case, my Ip is `10.212.134.200` – Eatsam ul haq Sep 01 '22 at 11:47
  • Which, as stated, is an internal IP range (see https://en.wikipedia.org/wiki/IP_address#Private_addresses) which only has meaning inside your own network. If you are calling it from your laptop (inside your own network) the IP Address is the internal IP address not some external one. So trying to convert that into a location will not work, you will need to call it from outside your network through the actual and real internet. – M. Deinum Sep 01 '22 at 12:01
  • 2
    If you test the IP address using [IPinfo/10.212.134.200](https://ipinfo.io/10.212.134.200) you will find this IP address is a bogon IP. – anyfactor Sep 01 '22 at 18:01
  • 10.x is a reserved IP address. It is not a bogon. – Michael C. Sep 02 '22 at 06:47
  • 1
    @MichaelC. Thank you for your reply. But, reserved IPs are categorized Bogon IPs. The [Wikipedia page](https://en.wikipedia.org/wiki/Bogon_filtering) on Bogon IP address explicitly mentions 10.X (reserved IP) as an example of Bogon. – anyfactor Sep 05 '22 at 18:19

2 Answers2

0

10.212.134.200 and 127.0.0.1 are addresses that have a meaning only inside your network, or inside your machine, not on the internet. There is no way for MaxMind to know where these are located. (You may be able to tell which city you are in by looking out of the window.)

If the request actually comes from the internet, so from outside your network, it is perhaps proxied. If that is the case, it is possible that the proxy has added a X-Forwarded-For or X-Real-IP request header or similar containing the external ("real") IP address of the original requester. You would feed that into getGeoLocation.

Wander Nauta
  • 18,832
  • 1
  • 45
  • 62
  • So how can I test my code. I deployed the application on server and then tried to access from my machine. In this case, my Ip is `10.212.134.200` – Eatsam ul haq Sep 01 '22 at 11:47
  • You need to query a 3rd party page such as ipaddress.my and get your public IP address. You have been getting private IP addresses using the codes. – Michael C. Sep 02 '22 at 06:48
0

Geo location based on IP may not give you original result. Say a user is on a VPN then you would not get user's original location. Based on the limited context I could grab, a better solution would be to ask for user's location permission on the client side which you could then send it over to server.

Naveen Kulkarni
  • 633
  • 6
  • 16