0

I want to store the URL/ip that I received the request from.

For example I am (server) receives the request from ip address 176.15.14.3.

I want to get that ip address 176.15.14.3

Can I do that with Spring.

1
  • 1
    check this and this Commented Nov 17, 2014 at 17:03

2 Answers 2

2

X-Forwarded-For value gives you the IP address of client. You can get the IP like below in spring.

String remoteAddress = request.getHeader("X-Forwarded-For");//request--HTTPServletRequest Object
            if (remoteAddress == null) {
                remoteAddress = request.getRemoteAddr();
            }
Sign up to request clarification or add additional context in comments.

1 Comment

Your answer is nice. And correct, if theyre using a proxy, but you should really make it clear that this wont work unless you're using a reverse proxy, to avoid confusion
1

Assuming you have access to the HttpServletRequest Object, just call response.getRemoteAddr() which returns the remote host's ip represented as a string.

See here

1 Comment

Unfortunately it's not gonna work if your server is behind a proxy - which is usually the case. @re350 answer is actually closer to perfect.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.