Main Tutorials

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 getClientIp() {

        String remoteAddr = "";

        if (request != null) {
            remoteAddr = request.getHeader("X-FORWARDED-FOR");
            if (remoteAddr == null || "".equals(remoteAddr)) {
                remoteAddr = request.getRemoteAddr();
            }
        }

        return remoteAddr;
    }

}

References

  1. How to get client Ip Address in Java

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
10 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
aaa
6 years ago

Not to say anything, but this wont even compile.. You cannot access request from static method since request is not static…

Vikram Thakur
6 years ago

This will not work. Its wrong !
The bean managed by spring is singleton by default and the request object injected will be a fixed request. So always you will get a common client IP address for the request which is first time injected.Instead of Injecting HttpServletRequest in WebUtils, you should take it as argument like getClientIp(HttpServletRequest request).

Rizwan Shaikh
6 years ago
Reply to  Vikram Thakur

I agree with you. This is not going to work.

Kumar Pallav
6 years ago

I dont know what Spring has to do in it , It is common Java API doing all work . HTTPServletRequest in Java no Spring , adding a component Yeah, but All Inboud Controller method do have access directly to HttpServletRequest

Dont comment when u dont know
4 years ago

The side is wok fine, HttpServert is a Session Scope injection

m e
4 years ago

I’m using this in production for blocking brute force and the truth is this is working fine! You are wrong!

Jay Yadav
5 years ago

Its not going to work because always request ipaddress will be same once HttpServletRequest @Autowired.

Drew
5 years ago

Probably should update your site…

Arow
5 years ago

That’s so bad. “”Developers”” showing beginners tutorials that are not even working… And I mean look at the start page of “Mykong.com”. It looks like its made from a child.

Casual Observer
3 years ago
Reply to  Arow

fwiw, your comments make you sound like a child. Just gross… Mkyong puts all this effort into his site only for you to complain about his hard work over an error and a style you don’t like? Grow up.