HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Tomcat / Spring, Tomcat – Get real IP behind load balancer

Spring, Tomcat – Get real IP behind load balancer

Mostly for logging and security purpose, we need the information of IP address for incoming requests. In any java web application, you can get IP address using getRemoteAddr() method.

String httpServletAddress = request.getRemoteAddr();

BUT, if your application is running behind a load balancer proxy and you would like to translate the real request IP that is used by the users instead of the IP from the proxy when our application instance receives the request – then above statement get get you IP address of balancer proxy only.

Use Tomcat’s RemoteIpFilter

In case of above situation, you may use tomcat provided RemoteIpFilter servlet filter.

To configure, RemoteIpFilter using java configuration – e.g. in spring boot application, register bean with @Configuration annotation.

@Configuration
public class WebConfiguration {
	@Bean
	public RemoteIpFilter remoteIpFilter() {
		return new RemoteIpFilter();
	}
}

Or using XML configuration e.g. web.xml – use filter tag.

<filter>
    <filter-name>RemoteIpFilter</filter-name>
    <filter-class>org.apache.catalina.filters.RemoteIpFilter</filter-class>
 </filter>
 
 <filter-mapping>
    <filter-name>RemoteIpFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
 </filter-mapping>

After registering above filter, start the application.

Now when you will use request.remoteAddr() method the you will get correct IP address of calling client.

Read More: RemoteIpFilter

Internally RemoteIpFilter integrate X-Forwarded-For and X-Forwarded-Proto HTTP headers.

Another feature of this servlet filter is to replace the apparent scheme (http/https) and server port with the scheme presented by a proxy or a load balancer via a request header (e.g. “X-Forwarded-Proto”).

Happy Learning !!

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Tushar

    June 18, 2020

    HI Lokesh,
    tell me, if i add catalina, juli, util like tomcat jars in other server and configure xml, it will work or not?

  2. Bill

    September 7, 2017

    Can this configuration work well with other web servers, such as Jetty?

    Thanks

    • Lokesh Gupta

      September 7, 2017

      RemoteIpFilter is tomcat specific. It will NOT work in other servers.

Comments are closed on this article!

Search Tutorials

Tomcat Tutorials

  • Tomcat – Architecture
  • Tomat – Default Port
  • Tomcat – Disable Directory Listing
  • Tomcat – SSL Configuration
  • Tomcat – Run Multiple Instances
  • Tomcat – Maven Plugin
  • Get Real IP Behind Load Balancer
  • How web servers work?

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)