1

Iv'e been using this guide: https://spring.io/guides/gs/rest-service to create a RESTFul web service, my issue I am having is that I do not know how to get information such as the clients IP address, is this possible with this API?

Thanks,

lug
  • 13
  • 1
  • 3
  • 2
    Possible duplicate of [How to extract IP Address in Spring MVC Controller get call?](https://stackoverflow.com/questions/22877350/how-to-extract-ip-address-in-spring-mvc-controller-get-call) – Edn Jul 13 '18 at 01:23

1 Answers1

1

In your Spring Rest Controller you can add HttpServletRequest to get client info.

Example

@GetMapping("/dummyurl")
public Boolean syncWithServNow(HttpServletRequest httpReq, @RequestParam("username") String username) {
    System.out.println(httpReq.getRemoteAddr());  // Line 1
}

In most cases it will work. In case like url is accessed by web server over a proxy server or has a load balancer this will do.

httpReq.getHeader("X-FORWARDED-FOR");
MyTwoCents
  • 7,284
  • 3
  • 24
  • 52