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 FreeAn 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:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Then, you can add a reference to the dependency:
xxxxxxxxxx
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.54</version>
</dependency>
</dependencies>
To install with Gradle, add the referenced to your root build.gradle at the end of repositories:
xxxxxxxxxx
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Then, add the dependency in build.gradle:
xxxxxxxxxx
dependencies {
implementation 'com.github.Cloudmersive:Cloudmersive.APIClient.Java:v3.54'
}
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:
xxxxxxxxxx
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.IpAddressApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();
// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
IpAddressApi apiInstance = new IpAddressApi();
String value = "value_example"; // String | IP address to check, e.g. \"55.55.55.55\". The input is a string so be sure to enclose it in double-quotes.
try {
IPThreatResponse result = apiInstance.iPAddressIsThreat(value);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling IpAddressApi#iPAddressIsThreat");
e.printStackTrace();
}
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:
xxxxxxxxxx
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.IpAddressApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();
// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
IpAddressApi apiInstance = new IpAddressApi();
String value = "value_example"; // String | IP address to check, e.g. \"55.55.55.55\". The input is a string so be sure to enclose it in double-quotes.
try {
TorNodeResponse result = apiInstance.iPAddressIsTorNode(value);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling IpAddressApi#iPAddressIsTorNode");
e.printStackTrace();
}
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:
xxxxxxxxxx
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.IpAddressApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();
// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
IpAddressApi apiInstance = new IpAddressApi();
String value = "value_example"; // String | IP address to geolocate, e.g. \"55.55.55.55\". The input is a string so be sure to enclose it in double-quotes.
try {
GeolocateResponse result = apiInstance.iPAddressPost(value);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling IpAddressApi#iPAddressPost");
e.printStackTrace();
}
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:
xxxxxxxxxx
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.IpAddressApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();
// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
IpAddressApi apiInstance = new IpAddressApi();
String value = "value_example"; // String | IP address to geolocate, e.g. \"55.55.55.55\". The input is a string so be sure to enclose it in double-quotes.
try {
GeolocateStreetAddressResponse result = apiInstance.iPAddressGeolocateStreetAddress(value);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling IpAddressApi#iPAddressGeolocateStreetAddress");
e.printStackTrace();
}
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.
Comments