Java Get Client IP Address
Posted By : Anil Kumar | 18-Dec-2017
Introduction:
In server side programming for security reasons we are required to get the client IP address and location details.
IP address play a key roles in getting the client/user account activity.
In the below example we used X-Forwarded-For to get client IP address.
Example:
package com.belfrics.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.belfrics.controller.AdminController;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
@Component
public class IPConfig {
private static HttpServletRequest request;
private static Logger LOGGER = LoggerFactory.getLogger(AdminController.class);
public GeoLocation result = null;
@Autowired
static GeoIPv4 geoIPv4;
@Autowired
public void setRequest(HttpServletRequest request) {
this.request = request;
}
public static String getIpAddr() {
String ip = "";
try (java.util.Scanner s = new java.util.Scanner(new java.net.URL("http://eth0.me/").openStream(), "UTF-8")
.useDelimiter("\\A")) {
ip = s.next();
} catch (java.io.IOException e) {
e.printStackTrace();
}
return ip;
}
public GeoLocation getClientIp() {
String remoteAddr = "";
String add = "";
try{
if (request != null) {
remoteAddr = request.getHeader("X-Forwarded-For");
if (remoteAddr == null || "".equals(remoteAddr)) {
remoteAddr = request.getRemoteAddr();
add = request.getRemoteHost();
LOGGER.info("IP local details"+ request.getRemoteAddr());
remoteAddr = request.getRemoteAddr()
if (remoteAddr.equalsIgnoreCase("0:0:0:0:0:0:0:1")) {
InetAddress inetAddress = InetAddress.getLocalHost();
String ipAddress = inetAddress.getHostAddress();
remoteAddr = ipAddress;
}
}
}
LOGGER.info("IP address is" + remoteAddr);
result = remoteAddr;
return result;
} catch(Exception e){
LOGGER.info(e.getMessage());
return null;
}
}
public static GeoLocation getLocation(String ipAddress) {
try{
Location result = lookUp.getLocation(ipAddress);
LOGGER.info("GeoIP getLocation: " + result);
if (result == null) {
return null;
}
return GeoLocation.map(result);
}catch(Exception e){
LOGGER.info(e.getMessage());
return null;
}
}
}
In this example if client ip address is fetched successfully it returns the IP address and if throw any exception
returns null value
This method now can be used in whole application by calling with the class name.
When a user login in the application we get their ip address and saved the details in database .
In the application we have created to many checks for recognize and block unsucipous activity from this account and on each and every login and logout of client send him the email with their ip details.
Request for Proposal
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Anil Kumar
Anil is a Web Developer who specializes in creating dynamic and beautiful web projects and has good experience of working in distributed teams.