DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Rethinking Java CRUDs With Event Sourcing and CQRS Patterns
  • Detecting Bugs and Vulnerabilities in Java With SonarQube
  • Introduction to Tactical DDD With Java: Steps to Build Semantic Code
  • AI Agents in Java: Architecting Intelligent Health Data Systems

Trending

  • Smart Deployment Strategies for Modern Applications
  • One Query, Four GPUs: Tracing a Distributed Training Stall Across Nodes
  • Why SAP S/4HANA Landscape Design Impacts Cloud TCO More Than Compute Costs
  • Securing Everything: Mapping the Right Identity and Access Protocol (OIDC, OAuth2, and SAML) to the Right Identity
  1. DZone
  2. Coding
  3. Java
  4. How to Validate and Geolocate an IP Address in Java

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.

By 
Brian O'Neill user avatar
Brian O'Neill
DZone Core CORE ·
Dec. 09, 20 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
7.3K Views

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: 

XML
 




x


 
1
<repositories>
2
    <repository>
3
        <id>jitpack.io</id>
4
        <url>https://jitpack.io</url>
5
    </repository>
6
</repositories>



Then, you can add a reference to the dependency:  

XML
 




xxxxxxxxxx
1


 
1
<dependencies>
2
<dependency>
3
    <groupId>com.github.Cloudmersive</groupId>
4
    <artifactId>Cloudmersive.APIClient.Java</artifactId>
5
    <version>v3.54</version>
6
</dependency>
7
</dependencies>



To install with Gradle, add the referenced to your root build.gradle at the end of repositories:  

Java
 




xxxxxxxxxx
1


 
1
allprojects {
2
    repositories {
3
        ...
4
        maven { url 'https://jitpack.io' }
5
    }
6
}



Then, add the dependency in build.gradle:  

Java
 




xxxxxxxxxx
1


 
1
dependencies {
2
        implementation 'com.github.Cloudmersive:Cloudmersive.APIClient.Java:v3.54'
3
}



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:  

Java
 




xxxxxxxxxx
1
24


 
1
// Import classes:
2
//import com.cloudmersive.client.invoker.ApiClient;
3
//import com.cloudmersive.client.invoker.ApiException;
4
//import com.cloudmersive.client.invoker.Configuration;
5
//import com.cloudmersive.client.invoker.auth.*;
6
//import com.cloudmersive.client.IpAddressApi;
7

          
8
ApiClient defaultClient = Configuration.getDefaultApiClient();
9

          
10
// Configure API key authorization: Apikey
11
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
12
Apikey.setApiKey("YOUR API KEY");
13
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
14
//Apikey.setApiKeyPrefix("Token");
15

          
16
IpAddressApi apiInstance = new IpAddressApi();
17
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.
18
try {
19
    IPThreatResponse result = apiInstance.iPAddressIsThreat(value);
20
    System.out.println(result);
21
} catch (ApiException e) {
22
    System.err.println("Exception when calling IpAddressApi#iPAddressIsThreat");
23
    e.printStackTrace();
24
}



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: 

Java
 




xxxxxxxxxx
1
24


 
1
// Import classes:
2
//import com.cloudmersive.client.invoker.ApiClient;
3
//import com.cloudmersive.client.invoker.ApiException;
4
//import com.cloudmersive.client.invoker.Configuration;
5
//import com.cloudmersive.client.invoker.auth.*;
6
//import com.cloudmersive.client.IpAddressApi;
7

          
8
ApiClient defaultClient = Configuration.getDefaultApiClient();
9

          
10
// Configure API key authorization: Apikey
11
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
12
Apikey.setApiKey("YOUR API KEY");
13
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
14
//Apikey.setApiKeyPrefix("Token");
15

          
16
IpAddressApi apiInstance = new IpAddressApi();
17
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.
18
try {
19
    TorNodeResponse result = apiInstance.iPAddressIsTorNode(value);
20
    System.out.println(result);
21
} catch (ApiException e) {
22
    System.err.println("Exception when calling IpAddressApi#iPAddressIsTorNode");
23
    e.printStackTrace();
24
}



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: 

Java
 




xxxxxxxxxx
1
24


 
1
// Import classes:
2
//import com.cloudmersive.client.invoker.ApiClient;
3
//import com.cloudmersive.client.invoker.ApiException;
4
//import com.cloudmersive.client.invoker.Configuration;
5
//import com.cloudmersive.client.invoker.auth.*;
6
//import com.cloudmersive.client.IpAddressApi;
7

          
8
ApiClient defaultClient = Configuration.getDefaultApiClient();
9

          
10
// Configure API key authorization: Apikey
11
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
12
Apikey.setApiKey("YOUR API KEY");
13
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
14
//Apikey.setApiKeyPrefix("Token");
15

          
16
IpAddressApi apiInstance = new IpAddressApi();
17
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.
18
try {
19
    GeolocateResponse result = apiInstance.iPAddressPost(value);
20
    System.out.println(result);
21
} catch (ApiException e) {
22
    System.err.println("Exception when calling IpAddressApi#iPAddressPost");
23
    e.printStackTrace();
24
}



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: 

Java
 




xxxxxxxxxx
1
24


 
1
// Import classes:
2
//import com.cloudmersive.client.invoker.ApiClient;
3
//import com.cloudmersive.client.invoker.ApiException;
4
//import com.cloudmersive.client.invoker.Configuration;
5
//import com.cloudmersive.client.invoker.auth.*;
6
//import com.cloudmersive.client.IpAddressApi;
7

          
8
ApiClient defaultClient = Configuration.getDefaultApiClient();
9

          
10
// Configure API key authorization: Apikey
11
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
12
Apikey.setApiKey("YOUR API KEY");
13
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
14
//Apikey.setApiKeyPrefix("Token");
15

          
16
IpAddressApi apiInstance = new IpAddressApi();
17
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.
18
try {
19
    GeolocateStreetAddressResponse result = apiInstance.iPAddressGeolocateStreetAddress(value);
20
    System.out.println(result);
21
} catch (ApiException e) {
22
    System.err.println("Exception when calling IpAddressApi#iPAddressGeolocateStreetAddress");
23
    e.printStackTrace();
24
}



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. 

Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Rethinking Java CRUDs With Event Sourcing and CQRS Patterns
  • Detecting Bugs and Vulnerabilities in Java With SonarQube
  • Introduction to Tactical DDD With Java: Steps to Build Semantic Code
  • AI Agents in Java: Architecting Intelligent Health Data Systems

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook