In this story, we will show how to get user information (devices, country, city, latitude, longitude) using an IP address.
In a production application, it is important as a user to check which computers, phones, and other devices have recently used your account, to ensure that no one other than you has signed into your account. To improve security, the user must be able to know the geographical position of the equipment which has used his account in order to be able to delete the connections on them in the event that the connections do not come from him.
In this article, we will explore how to get geographic location data from an IP address using the MaxMind GeoIP2 Java API with the free GeoLite2 database.
GeoLite2 Databases
GeoLite2 databases are free IP geolocation databases comparable to, but less accurate than, MaxMind’s GeoIP2 databases. The GeoLite2 Country, City, and ASN databases are updated weekly, every Tuesday. GeoIP2 supports many different programming languages such as: .NET (C#), C, Java, Node.js, Ruby, PHP, Python, …
There are two ways to work with GeoIP2 API:
- Download GeoLite2 with location data and use it as a local database. GeoIP2 API will retrieve geographic information from this database.
- Use GeoIP2 API to connect to web service which provides location information via IP address, by this way you need a License_Key (must buy).
Downloading the Database
First, signup for a MaxMind account, which is now required to download even free/public GeoIP databases.
Once you’ve set up an account, login to your account portal and select “Download Files” to access the databases.
Then, you can download databases. download the GeoLite2 City database and decompress it.

Using the GeoIP2 Java API
Create a new Spring boot Project with Maven and include the maxmind GeoIP2 dependency to your pom.xml:
<dependency>
<groupId>com.maxmind.geoip2</groupId>
<artifactId>geoip2</artifactId>
<version>2.14.0</version>
</dependency>
You can find all versions here.
You have already downloaded the GeoLite2 database in the previous step. unzip GeoLite2 City in the resources folder.

To use the database API, you must create a new DatabaseReader using the DatabaseReader.Builder. You must provide the Builder constructor either an InputStream or File for your GeoIP2 database. You may also specify the fileMode and the locales fallback order using the methods on the Builder object. — maxmind github

The next step is to write the class of service that retrieves the geolocation data using the Java API GeoIP2 and GeoLite2 database. This class has a main method getIpLocation(String ip, HttpServletRequest request).

Finally, add the Controller class which sends the “ipAddress” request parameter to our service class to get the geolocation response data

Final Result
After starting the application, open your web browser and test with:
http://localhost:8080/geoIP/<YOUR_IP_ADDRESS>

In production mode, you can extract the IP address from HttpServletRequest without passing it as a parameter
The complete source code can be found in my GitHub repository.