How to Validate and Geolocate an IP Address in Java
Check for possible threats or tor servers, as well as geolocating an IP address to its general area or specific address.
Join the DZone community and get the full member experience.Join For Free
An IP Address is used as a unique identifier for network-connected hardware such as computers and smartphones. These contain four sets of numbers that differentiate each device when it accesses network services such as the internet. This information can be very useful for businesses with websites, as they can verify their user’s various IP Addresses to gather important client-specific and audience information for various purposes.
Some of the most important usages for IP Addresses is threat tracking and user data such as identifying Tor servers and providing location information that can assist with security and user-experience needs. The following four APIs are centered around these functions and can be used separately or in tandem. This will allow you to ensure the security of your site, as well as providing data that can help optimize your platforms for users.
To use any of the following APIs, you will first need to install the SDK library using Maven or Gradle. To install with Maven, first add a Jitpack reference to the repository in pom.xml:
Then, you can add a reference to the dependency:
To install with Gradle, add the referenced to your root build.gradle at the end of repositories:
Then, add the dependency in build.gradle:
The first function will validate an IP address and check whether it is a known threat. The address will be compared against known IPs, botnets, compromised servers, and other lists of threats but simply inputting its numerical value as a string. After installing the SDK library, you can call the function:
This will return whether the address is a threat and what type of threat it presents. To ensure that this API works properly, you need to ensure certain requirements are met:
- The IP Address is input correctly.
- You have input your API Key. This can be retrieved at no cost on the Cloudmersive website, providing 800 monthly calls across our API library.
To check for Tor servers, you can use the next function. Tor servers are a type of privacy-preserving technology that can hid the original IP Address of a user. While this can be useful for users who do not want their information collected via the IP Address, it removes a layer of security for the sites they use. Install the SDK library as shown above, and call the function:
This will return whether a Tor Node server was detected.
For users without this kind of privacy server, an IP Address can be used to provide location information for a device, which organizations can then use for marketing and user experience purposes. The following two APIs will identify this information, with the first being a more general search, while the second can identify location down to the Street Address of a user.
To geolocate an IP Address to its local area, install the SDK library and call the function below:
This will return the country name and code, city, region name and code, Zip code, time zone name, and latitude and longitude for the IP Address.
To retrieve more specific information on the location of an IP Address, you can use this next API:
This will return the country code and name, street address, city, region name, and Zip code.
With these, you can not only protect your site and its users but also provide the best experience possible.
Opinions expressed by DZone contributors are their own.