Rest - how get IP address of caller

前端 未结 5 1188
离开以前
离开以前 2021-02-05 04:11

I am writing a Java Rest Web Service and need the caller\'s IP Address. I thought I saw this in the cookie once but now I don\'t see it. Is there a consistent place to get this

相关标签:
5条回答
  • 2021-02-05 04:42

    Assuming you are making your "web service" with servlets, the rather simple method call .getRemoteAddr() on the request object will give you the callers IP address.

    0 讨论(0)
  • 2021-02-05 04:44

    If your application is running on a webserver that is located behind a reverse proxy or load balancer, then that proxy can be configured to inject the requested IP address in a request header. Different reverse proxies can inject different headers. Consult the documentation for your proxy server. We listed a couple of the most used in our example below but this is by no means a complete list. When your client uses a (forward) proxy, then it might insert headers to say what the client IP addres is. Or it might not. And the IP address inserded here might be incorrect. This means that the value you get by calling request.getRemoteAddr() is the IP address of the immediate upstream source of the request. As we said, there are many headers for different proxies in use, but x-forwareded-for is most likely to be inserted by a proxy. As a last note, even if you get an IP address either from the header or from request.getRemoteAddr() it is not guarenteed to be the client IP address. e.g.: if your proxy does not include the IP address of the client then you’ll get the IP address of the proxy or load balancer. If your client works on a private network and connect to the internet via a NAT gateway, then the IP address in the HTTP request will be an address of the NAT server. Or even for a hacker it is quite easy to inject a header with a different IP address. So this means that you cannot reliably find out the IP address of the system that the request originated from.

     private static final String[] IP_HEADER_CANDIDATES = { 
            "X-Forwarded-For",
            "Proxy-Client-IP",
            "WL-Proxy-Client-IP",
            "HTTP_X_FORWARDED_FOR",
            "HTTP_X_FORWARDED",
            "HTTP_X_CLUSTER_CLIENT_IP",
            "HTTP_CLIENT_IP",
            "HTTP_FORWARDED_FOR",
            "HTTP_FORWARDED",
            "HTTP_VIA",
            "REMOTE_ADDR" };
    
        public static String getClientIpAddress(HttpServletRequest request) {
            for (String header : IP_HEADER_CANDIDATES) {
                String ip = request.getHeader(header);
                if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
                    return ip;
                }
            }
            return request.getRemoteAddr();
        }
    
    0 讨论(0)
  • 2021-02-05 04:56

    You could do something like this:

    @WebService
    public class YourService {
    
       @Resource
       WebServiceContext webServiceContext; 
    
       @WebMethod 
       public String myMethod() { 
    
          MessageContext messageContext = webServiceContext.getMessageContext();
          HttpServletRequest request = (HttpServletRequest) messageContext.get(MessageContext.SERVLET_REQUEST); 
          String callerIpAddress = request.getRemoteAddr();
    
          System.out.println("Caller IP = " + callerIpAddress); 
    
       }
    }
    
    0 讨论(0)
  • 2021-02-05 05:01

    I think you can get the IP through the request object.

    If I'm not mistaken, request.getRemoteAddr() or so.

    0 讨论(0)
  • 2021-02-05 05:04

    Inject a HttpServletRequest into your Rest Service as such:

    import javax.servlet.http.HttpServletRequest;
    
    @GET
    @Path("/yourservice")
    @Produces("text/xml")
    public String activate(@Context HttpServletRequest requestContext,@Context SecurityContext context){
    
       String ipAddressRequestCameFrom = requestContext.getRemoteAddr();
    
       //Also if security is enabled
       Principal principal = context.getUserPrincipal();
       String userName = principal.getName();
    
    }
    
    0 讨论(0)
提交回复
热议问题