1

Is it possible to get the IP of the machine from which the request is coming from using Spring RestTemplate. I have a Spring Boot web application, in which I want to log all the requests with the machine IP from where it is coming from. Is this possible my any means?

Also, is it possible to get the client username.

Sarath
  • 1,438
  • 4
  • 24
  • 40
  • 2
    try to access the HttpServletRequest : `request.getRemoteAddr()`, here you can find hints on how to do it : http://stackoverflow.com/questions/8504258/spring-3-mvc-accessing-httprequest-from-controller – francesco foresti May 13 '16 at 07:48
  • 1
    be aware that there is no reliable mechanism to do this. If the request is mediated by a proxy you have no guarantee that the client IP address is accessible. – MaVVamaldo May 13 '16 at 07:52
  • 1
    If you're using Tomcat or Undertow, have you considered [enabling access logging](http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-configure-accesslogs)? – Andy Wilkinson May 13 '16 at 08:56
  • @francescoforesti : when I tried request.getRemoteAddr(), it is returning the IP of localhost (127.0.0.1) request.getRemoteUser() -> returns null request.getRemoteHost() -> returns 127.0.0.1 request.getRemoteAddr() -> returns 127.0.0.1 – Sarath May 18 '16 at 08:46
  • are you invoking the service from the same machine the service is hosted on? If so, there are multiple ip addresses available, and you are only seeing the first one. Try to access the service from another machine and see if the result changes. Also, you could try to see if there is anything of interest in the request headers (e.g. proxies changing the ip address of the client) via `request.getHeaderNames()` and `request.getHeaders(name)`. – francesco foresti May 18 '16 at 09:02
  • @francescoforesti : I tried as you said. These are the headers i see **host, connection, content-length, accept, origin, x-requested-with, user-agent, content-type, referer, accept-encoding, accept-language** Note: Its a spring-boot mvc application. – Sarath May 18 '16 at 09:07
  • 1
    @francescoforesti : When requesting from a different machine, it did show the ip of the client IP. Thanks for your help. – Sarath May 18 '16 at 10:04

0 Answers0