1

I have a java REST web service which is deployed at Microsoft Azure. I want to log the IP address of the caller whoever is hitting my API. Problem is that if I will simply get the IP from remoteaddress then it will give the Azure's default IP. How to capture the original IP of the caller.

Anuj Sharma
  • 111
  • 1
  • 1
  • 9
  • See [**this answer**](https://stackoverflow.com/questions/48618450/get-client-ip-in-azure-through-mvc-5-controller/48618538#48618538). You need to parse the `x-forwarded-for` header. – evilSnobu May 08 '18 at 07:35
  • you can use `HttpServletRequest.getRemoteAddr()` to get the client’s IP address that’s accessing your Java web, and get the client IP address via the HTTP request header X-Forwarded-For (XFF). Refer to this [article about How to get client Ip Address in Java](https://www.mkyong.com/java/how-to-get-client-ip-address-in-java/). – Joey Cai May 08 '18 at 07:45
  • Hi,any updates now? Does my answer helps you? – Jay Gong May 17 '18 at 01:17

1 Answers1

0

As @Joey mentioned in the comment, you could follow the doc to get the client's ip address in your web app.

I tested in my azure spring-boot web app ,please refer to my sample code:

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;

@RestController
public class HelloController {
@RequestMapping("/getip")
    public String getIp(HttpServletRequest request) {
        return getIpAddr(request);
    }


    public static String getIpAddr(HttpServletRequest request) {
        String ipAddress = null;
        try {
            ipAddress = request.getHeader("x-forwarded-for");
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getRemoteAddr();
                if (ipAddress.equals("127.0.0.1")) {

                    InetAddress inet = null;
                    try {
                        inet = InetAddress.getLocalHost();
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    }
                    ipAddress = inet.getHostAddress();
                }
            }

            if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
                // = 15
                if (ipAddress.indexOf(",") > 0) {
                    ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
                }
            }
        } catch (Exception e) {
            ipAddress = "";
        }
        // ipAddress = this.getRequest().getRemoteAddr();

        return ipAddress;
    }
}

Hope it helps you.

Jay Gong
  • 23,163
  • 2
  • 27
  • 32