0

I need to capture public ip of the system calling our api, that system is a desktop Application thats calling our rest api and I am using the following code

    @RequestMapping(value = "/api", method = RequestMethod.POST)
    public @ResponseBody JSON_Response sendMessage(@RequestBody Objectjson objjson,HttpServletRequest
            request) {

        LOG.debug("sending message via payless server");
        inputjson.setSentFrom("Message Sent From Api");
    //  inputjson.setHostip(""+request.getRemoteHost());
//I am using following to capture it
        LOG.debug("--------------------------"+request.getRemoteUser());
        LOG.debug("--------------------------"+request.getLocalAddr());
        LOG.debug("--------------------------"+request.getRemoteHost());
        LOG.debug("--------------------------"+request.getPathInfo());
        LOG.debug("--------------------------"+request.getPathTranslated());
        LOG.debug("--------------------------"+request.getRemoteUser());
        LOG.debug("--------------------------"+request.getRemoteUser());
        LOG.debug("--------------------------"+request.getRemoteUser());
        LOG.debug("--------------------------"+request.getRemoteUser());
        LOG.debug("--------------------------"+request.getRemoteUser());
        JSON_Response response = sendMessageInterface.processInput(inputjson);
        LOG.debug("sending response message " + response.getStatusDescription());

        return response;
    }

I am getting my own server ip in the ip address.If i call the rest api from postman i am getting the correct ip address.

Please let me know if you find any other way to retrieve public ip .

I am using Wildfly Server wildfly-10.1.0.Final

kirti
  • 4,499
  • 4
  • 31
  • 60
  • Possible duplicate of [Is it possible to accurately determine the IP address of a client in java servlet](https://stackoverflow.com/questions/9326138/is-it-possible-to-accurately-determine-the-ip-address-of-a-client-in-java-servle) – spi Dec 11 '17 at 13:24
  • 1
    Also - why are you calling `request.getRemoteUser()` 6 times? – achAmháin Dec 11 '17 at 13:27
  • In short try this: request.getRemoteAddr() https://stackoverflow.com/questions/22877350/how-to-extract-ip-address-in-spring-mvc-controller-get-call – Shane Burroughs Dec 11 '17 at 17:15

1 Answers1

1

This is the method that i Use to get the remote user IP address. Hope it helps

public HttpServletRequest getRequest() {
  RequestAttributes attribs = RequestContextHolder.getRequestAttributes();
  if (attribs instanceof ServletRequestAttributes) {
    HttpServletRequest request = ((ServletRequestAttributes)attribs).getRequest();
    return request;
  }
  return null;
}

public String getClientIp() {
  String remoteAddr = "";
  HttpServletRequest request = getRequest();
  if (request != null) {
    remoteAddr = request.getHeader("X-FORWARDED-FOR");
    if (remoteAddr == null || remoteAddr.trim().isEmpty()) {
      remoteAddr = request.getRemoteAddr();
    }
  }
  return remoteAddr;
}
locus2k
  • 2,802
  • 1
  • 14
  • 21