Spring Boot - get client IP address from request (HttpServletRequest)

10 points
Created by:

In this short article, we would like to show how to get a client IP address from a request in Spring Boot Web Application in Java.

Client IP Address for request in local network - Spring Boot Application
Client IP Address for request in local network - Spring Boot Application


  1. Integrate below code with your web site,
  2. Run your Spring Boot Web Application,
  3. Open one of the following addresses in the web browser:
    • http://localhost/client-ip-address for development (on localhost),
    • for production. file:

package com.example.controllers;

import com.example.utils.HttpUtils;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

public class ClientIPAddressController {

        method = RequestMethod.GET,
        value = "/client-ip-address",
        produces = MediaType.TEXT_PLAIN_VALUE
    public String getClientIPAddress(HttpServletRequest request) {
        String ip = HttpUtils.getRequestIP(request);
        return "Client IP Address: " + ip;
} file:

package com.example.utils;

import javax.servlet.http.HttpServletRequest;

public final class HttpUtils {

    private static final String[] IP_HEADERS = {

        // you can add more matching headers here ...

    private HttpUtils() {
        // nothing here ...

    public static String getRequestIP(HttpServletRequest request) {
        for (String header: IP_HEADERS) 
            String value = request.getHeader(header);
            if (value == null || value.isEmpty()) {
            String[] parts = value.split("\\s*,\\s*");
            return parts[0];
        return request.getRemoteAddr();

Note: IP_HEADERS are useful to get a client IP address when we use some other application that may cover our server, e.g. VirtualServers, load balancers, etc.


Alternative titles

  1. Java - get user IP in Spring Boot 2
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Spring Boot

Native Advertising
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂