简体   繁体   English

休息 - 如何获取来电者的 IP 地址

[英]Rest - how get IP address of caller

I am writing a Java Rest Web Service and need the caller's IP Address.我正在编写 Java Rest Web 服务并且需要调用者的 IP 地址。 I thought I saw this in the cookie once but now I don't see it.我以为我曾经在 cookie 中看到过这个,但现在我没有看到。 Is there a consistent place to get this information?是否有一致的地方可以获取这些信息?

I saw one example of using an "OperationalContext" to get it but that was not in java.我看到了一个使用“OperationalContext”来获取它的例子,但这不是在java中。

Inject a HttpServletRequest into your Rest Service as such:HttpServletRequest注入您的 Rest 服务,如下所示:

import javax.servlet.http.HttpServletRequest;

@GET
@Path("/yourservice")
@Produces("text/xml")
public String activate(@Context HttpServletRequest requestContext,@Context SecurityContext context){

   String ipAddressRequestCameFrom = requestContext.getRemoteAddr();
   // 
   String xForwardedForIP = req.getHeader("X-Forwarded-For");
   
   //Also if security is enabled
   Principal principal = context.getUserPrincipal();
   String userName = principal.getName();

}

As @Hemant Nagpal mentions, you can also check the X-Forwarded-For header to determine the real source if a load balancer inserts this into the request.正如@Hemant Nagpal 所提到的,如果负载均衡器将其插入到请求中,您还可以检查X-Forwarded-For标头以确定真正的来源。 According to this answer , the getHeader() call is case insensitive.根据这个答案getHeader()调用不区分大小写。

I think you can get the IP through the request object.我认为您可以通过请求对象获取IP。

If I'm not mistaken, request.getRemoteAddr() or so.如果我没记错的话, request.getRemoteAddr()左右。

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); 

   }
}

假设您正在使用 servlet 制作“Web 服务”,请求对象上的相当简单的方法调用.getRemoteAddr()将为您提供调用方的 IP 地址。

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.如果您的应用程序在位于反向代理或负载均衡器后面的网络服务器上运行,则可以将该代理配置为在请求标头中注入请求的 IP 地址。 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.当您的客户端使用(转发)代理时,它可能会插入标头来说明客户端 IP 地址是什么。 Or it might not.或者它可能不会。 And the IP address inserded here might be incorrect.而且这里插入的IP地址可能不正确。 This means that the value you get by calling request.getRemoteAddr() is the IP address of the immediate upstream source of the request.这意味着您通过调用 request.getRemoteAddr() 获得的值是请求的直接上游源的 IP 地址。 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.正如我们所说,使用的不同代理有许多标头,但 x-forwareded-for 最有可能由代理插入。 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.最后要注意的是,即使您从标头或从 request.getRemoteAddr() 获得 IP 地址,也不能保证是客户端 IP 地址。 eg: 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.例如:如果您的代理不包含客户端的 IP 地址,那么您将获得代理或负载均衡器的 IP 地址。 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.如果您的客户端在专用网络上工作并通过 NAT 网关连接到 Internet,那么 HTTP 请求中的 IP 地址将是 NAT 服务器的地址。 Or even for a hacker it is quite easy to inject a header with a different IP address.或者甚至对于黑客来说,注入具有不同 IP 地址的标头也很容易。 So this means that you cannot reliably find out the IP address of the system that the request originated from.因此,这意味着您无法可靠地找出发出请求的系统的 IP 地址。

 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();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM