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.

bula
  • 8,719
  • 5
  • 27
  • 44
  • 1
    check [this](http://stackoverflow.com/questions/22877350/how-to-extract-ip-address-in-spring-mvc-controller-get-call) and [this](http://stackoverflow.com/questions/8926001/access-to-request-source-ip-in-spring-3-mvc-web-application) – Prasad Khode Nov 17 '14 at 17:03

2 Answers2

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();
            }
dReAmEr
  • 6,986
  • 7
  • 36
  • 63
  • 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 – Zachary Craig Nov 18 '14 at 07:47
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

Zachary Craig
  • 2,192
  • 4
  • 23
  • 34
  • 1
    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. – Maciej Walkowiak Nov 17 '14 at 22:14