In order to get the IP address of a Client you can make use of getRemoteAddr method from the HttpServletRequest class from javax.servlet.http package,
The tricky part here is there could be multiple headers the client might be behind the proxy servers or load balancers,
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
/**
* Java Program to get Client
* IP address
*
* Author: Code2care.org
* Date: 03-Apr-2022
* Version: 1.0
*
*/
public class JavaExample {
public static void main(String[] args) throws UnknownHostException {
System.out.println(getRemoteClientIPAddress());
}
private static String getRemoteClientIPAddress(HttpServletRequest request) {
List<String> proxyHeadersList = new ArrayList();
//There could be much more
proxyHeadersList.add("X-Forwarded-FOR");
proxyHeadersList.add("WL-Proxy-Client-IP");
proxyHeadersList.add("Proxy-Client-IP");
proxyHeadersList.add("HTTP_X_FORWARDED");
String remoteIpAddress;
for(String proxyHeader: proxyHeadersList) {
remoteIpAddress = request.getHeader("X-FORWARDED-FOR");
if (isValidIP(remoteIpAddress)) {
return remoteIpAddress;
}
}
return request.getRemoteAddr();
}
public static boolean isValidIP(String remoteIpAddress) {
if (remoteIpAddress == null || "".equals(remoteIpAddress)) {
return false;
} else {
return true;
}
}
}
More Posts related to Java,
- Add two numbers using Java Generics
- Convert Java List to Json String using Jackson
- Convert Java Object to JSON using Jackson Library
- Java SE JDBC: Insert with PreparedStatement Example
- [Program] How to read three different values using Scanner in Java
- Java JDBC Batch Update Example with PreparedStatement
- Java Stream flatmap() Examples
- Save Java Object as JSON file using Jackson Library
- Java get day of the week as an int using DayOfWeek
- Create Nested Directories using Java Code
- Java JDBC Delete a Record in Database Table using PreparedStatement
- List of jars required for Struts2 project
- Convert Java Object to XML using Jackson Library
- Struts2 : java.lang.ClassNotFoundException: org.apache.commons.fileupload.RequestContext
- Java JDBC Get Id of the Inserted Record with AutoIncrement
- How to list all tables using Java JDBC
- Java Jackson ObjectMapper Class with Examples
- Fix: Maven - Failed to execute goal - Compilation failure - Source/Target option 5 is no longer supported. Use 7 or later
- Eclipse : The type java.lang.CharSequence cannot be resolved. Indirectly referenced from required .class files
- Formatting Double in Java [Examples]
- How to run Java Unit Test cases with Apache Maven?
- [fix] NullPointerException Cannot Invoke findById because Repository is null - Java Spring
- [Fix] java: integer number too large compilation error
- [Java] Read a File with UTF-8 Encoding
- How to detect Operating System using Java code
More Posts:
- How to Search Something (string) in Android Studio Project like Eclipse - Android-Studio
- airbnb website and mobile App is down worldwide - clear browser cache or reinstall app to fix error - News
- Java 8 foreach loop code examples - Java
- Android Toolbar example with appcompat_v7 21 - Android
- Install OpenJDK Java (8/11/17) versions using brew on Mac (Intel/M1/M2) - MacOS
- How to turn off Location Services macOS Ventura 13 - MacOS
- How to clear Cache, Cookies and History on iPhone Safari - iOS
- What does b prefix before a String mean in Python? - Python