Spring Boot Web Get Client IP Address

Tags: Java Spring Boot IP

Introduction

In this article we are going to learn how to obtain the client’s browser IP address when a user visits our Spring Boot web application. The example Java source code also shows how to get the client IP address even when the application deployed behind the proxy server. We also get the IP address of the local network if the client visits the Spring Boot website via local network.

Table of contents

  1. Introduction
  2. Create Spring Boot Web Project
  3. Create Request Service interface
  4. Implement Request service to get Client’s IP Address
  5. Implement Controller and view to show Client IP address
  6. Final application
  7. Video Spring Boot Web Get Client IP Address
  8. Download Source Code

Create Spring Boot Web Project

From Spring Tool Suite IDE select menu File > New > Spring Starter Project.

On the New Spring Starter Project popup input new project spring-boot-client-ip information as following screenshot.

Create Spring Boot Web Project Get Client IP Address

On the New Spring Starter Project Dependencies popup choose Thymeleaf and Spring Web dependency as below screenshot.

Create Spring Boot Web Project Get Client IP Address

You can also creating new Spring Boot project using Spring initializr online tool at start.spring.io

Create Request Service interface

Create a new Java source package named dev.simplesolution.ip.service, then add a new interface RequestService with the following definition.

package dev.simplesolution.ip.service;

import javax.servlet.http.HttpServletRequest;

public interface RequestService {
	
	String getClientIp(HttpServletRequest request);
	
}

Implement Request service to get Client’s IP Address

Create a new Java package named dev.simplesolution.ip.service.impl and implement RequestServiceImpl class.

package dev.simplesolution.ip.service.impl;

import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import dev.simplesolution.ip.service.RequestService;

@Service
public class RequestServiceImpl implements RequestService {

	private final String LOCALHOST_IPV4 = "127.0.0.1";
	private final String LOCALHOST_IPV6 = "0:0:0:0:0:0:0:1";
	
	@Override
	public String getClientIp(HttpServletRequest request) {
		String ipAddress = request.getHeader("X-Forwarded-For");
		if(StringUtils.isEmpty(ipAddress) || "unknown".equalsIgnoreCase(ipAddress)) {
			ipAddress = request.getHeader("Proxy-Client-IP");
		}
		
		if(StringUtils.isEmpty(ipAddress) || "unknown".equalsIgnoreCase(ipAddress)) {
			ipAddress = request.getHeader("WL-Proxy-Client-IP");
		}
		
		if(StringUtils.isEmpty(ipAddress) || "unknown".equalsIgnoreCase(ipAddress)) {
			ipAddress = request.getRemoteAddr();
			if(LOCALHOST_IPV4.equals(ipAddress) || LOCALHOST_IPV6.equals(ipAddress)) {
				try {
					InetAddress inetAddress = InetAddress.getLocalHost();
					ipAddress = inetAddress.getHostAddress();
				} catch (UnknownHostException e) {
					e.printStackTrace();
				}
			}
		}
		
		if(!StringUtils.isEmpty(ipAddress) 
				&& ipAddress.length() > 15
				&& ipAddress.indexOf(",") > 0) {
			ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
		}
		
		return ipAddress;
	}
	
}

Implement Controller and view to show Client IP address

Create a new Java package named dev.simplesolution.ip.controller and add the controller class HomeController.

package dev.simplesolution.ip.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import dev.simplesolution.ip.service.RequestService;

@Controller
public class HomeController {
	
	@Autowired
	private RequestService requestService;
	
	@RequestMapping("/")
	public ModelAndView index(HttpServletRequest request) {
		ModelAndView model = new ModelAndView("index");
		String clientIp = requestService.getClientIp(request);
		model.addObject("clientIp", clientIp);
		return model;
	}

}

Add index.html file at \src\main\resources\templates\index.html and implement Thymeleaf view as following code.

<!DOCTYPE html>
<html>
	<head>
		<title>Client IP</title>
	</head>
	<body>
		<h1>Your IP Address: <span th:text="${clientIp}"></span></h1>
	</body>
</html>

Final application

Your final Spring Boot web application will look like the following structure.

Final Spring Boot Web Project Get Client IP Address

Run the Spring Boot application and visit the web application in your local browser at localhost:8080

Demo Spring Boot Web Project Get Client IP Address

Video Spring Boot Web Get Client IP Address

Download Source Code

The source code in this article can be found at: github.com/simplesolutiondev/spring-boot-client-ip

or clone at:

git clone https://github.com/simplesolutiondev/spring-boot-client-ip.git

or download at:

Download Source Code

Happy Coding 😊

Java Get Local Host IP Address

Java Get Local Host Name

Creating Spring Boot Application with Spring Tool Suite

Spring Boot Get Base URL

Spring Boot Web with JSP

Spring Boot Console Application using CommandLineRunner