This tutorial provides examples of how to get the client IP address from an HTTP request in Spring Boot.
Spring Boot is a popular web framework for creating services. In some cases, you may need to get the IP address of a client or user that makes an HTTP request to your server. For example, if you need to log the IP address of important requests or limit resource access based on the IP address. This tutorial is divided into two parts, for Spring MVC and for Spring WebFlux.
Spring MVC
In Spring MVC, you can get the request information from the HttpServletRequest
class. In the controller method, add a parameter whose type is HttpServletRequest
. The client IP address can be obtained from the getRemoteAddr()
method.
@GetMapping
public void test(HttpServletRequest request) {
String remoteAddress = request.getRemoteAddr();
System.out.println(remoteAddress);
System.out.println(getIpAddressFromHeader(request));
}
Another way to get the IP address is from the HTTP header. Below are the common header names that you may need to look for.
private static final List<String> POSSIBLE_IP_HEADERS = List.of(
"X-Forwarded-For",
"HTTP_FORWARDED",
"HTTP_FORWARDED_FOR",
"HTTP_X_FORWARDED",
"HTTP_X_FORWARDED_FOR",
"HTTP_CLIENT_IP",
"HTTP_VIA",
"HTTP_X_CLUSTER_CLIENT_IP",
"Proxy-Client-IP",
"WL-Proxy-Client-IP",
"REMOTE_ADDR"
);
From the HttpServletRequest
object, you can get the list of header values with a specified name by calling the getHeaders
method with the header name as the argument. Then, filter non empty values which should contain the IP address. It would be even better if you are able to check whether the value is a valid IP address. If you don't know the header name, you need to iterate over a list of possible header names. But, if you are sure that the IP address value is always present on a specific header (for example, if your load balancer always adds the IP address to a header), you can directly get the value from the specific header.
private String getIpAddressFromHeader(HttpServletRequest request) {
for (String ipHeader : POSSIBLE_IP_HEADERS) {
String headerValue = Collections.list(request.getHeaders(ipHeader)).stream()
.filter(StringUtils::hasLength)
.findFirst()
.orElse(null);
if (headerValue != null) {
return headerValue;
}
}
return null;
}
Spring WebFlux
In Spring WebFlux, the request information is available from the ServerHttpRequest
object (in org.springframework.http.server.reactive
package). So, you need to add a ServerHttpRequest
parameter in the controller method, instead of HttpServletRequest
. The ServerHttpRequest
has a getRemoteAddress
method which returns InetSocketAddress
. The InetSocketAddress
has some properties to get the components of an address.
getAddress()
: Returns theInetAddress
object. If you print anInetAddress
object, you'll get the host name and the host address separated by/
.getHostName()
: Returns the hostname. If you create the address using IP literal, it may trigger a name service reverse lookup.getHostString()
: If the hostname is available, returns the hostname. Otherwise, it returns the IP address as a string. UnlikegetHostName()
, it will not trigger a name service reverse lookup. It may return the hostname if you create the address using the hostname, or if you already performgetHostName()
operation on the object.getPort()
: Returns the port number.
@GetMapping
public void test(ServerHttpRequest request) {
InetSocketAddress remoteAddress = request.getRemoteAddress();
System.out.println(remoteAddress.getAddress());
System.out.println(remoteAddress.getHostName());
System.out.println(remoteAddress.getHostString());
System.out.println(remoteAddress.getPort());
}
Another alternative is by extracting the value from the request header. From the ServerHttpRequest
object, you can get the values of a header by calling getHeaders().getValuesAsList(headerName)
. If you are not sure what is the header name that contains the IP address, you need to iterate over a list of possible header names.
private String getIpAddressFromHeader(ServerHttpRequest request) {
for (String ipHeader : POSSIBLE_IP_HEADERS) {
String headerValue = request.getHeaders()
.getValuesAsList(ipHeader)
.stream()
.filter(StringUtils::hasLength)
.findFirst()
.orElse(null);
if (headerValue != null) {
return headerValue;
}
}
return null;
}
Summary
In Spring MVC, the IP address information is available from the getRemoteAddr
method of HttpServletRequest
. In Spring WebFlux, it can be extracted from the getRemoteAddress
method of ServerHttpRequest
. Another alternative is by reading the value of a request header.
You can also read about: