Spring MVC – How to get client IP address

In Spring framework, you can @Autowired a HttpServletRequest in any Spring managed bean directly, and later get the client’s IP address from the request headers WebUtils.java package com.mkyong.web; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; @Component public class WebUtils { private HttpServletRequest request; @Autowired public void setRequest(HttpServletRequest request) { this.request = request; } private static String …

Read more

How To Get HTTP Request Header In Java

This example shows you how to get the HTTP request headers in Java. To get the HTTP request headers, you need this class HttpServletRequest : 1. HttpServletRequest Examples 1.1 Loop over the request header’s name and print out its value. WebUtils.java package com.mkyong.web.utils; import javax.servlet.http.HttpServletRequest; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; public class WebUtils { …

Read more

How to get client Ip Address in Java

In Java, you can use HttpServletRequest.getRemoteAddr() to get the client’s IP address that’s accessing your Java web application. import javax.servlet.http.HttpServletRequest; String ipAddress = request.getRemoteAddr(); 1. Proxy Server or Cloudflare For web application which is behind a proxy server, load balancer or the popular Cloudflare solution, you should get the client IP address via the HTTP …

Read more