简体   繁体   English

如何在ejb呼叫中获取主叫IP地址?

[英]How can you get the calling ip address on an ejb call?

If a java client calls a remote EJB on a different server, how can you get the client IP address? 如果Java客户端在另一台服务器上调用远程EJB,您如何获得客户端IP地址? Note that it is important to get it from the server, because the client is likely behind a NAT firewall, and in this case we need the public IP address. 请注意,从服务器获取它很重要,因为客户端可能位于NAT防火墙之后,在这种情况下,我们需要公共IP地址。

NOTE: Although it would preferably be a generic solution, at a minimum I could use one that retrieves the IP address from an EJB2 call on JBoss 4.2.2 注意:尽管最好是通用解决方案,但至少我可以使用一个从JBoss 4.2.2上的EJB2调用中检索IP地址的解决方案。

This article on the JBoss community wiki addresses exactly your issue. JBoss社区Wiki上的这篇文章完全解决了您的问题。 Prior to JBoss 5 the IP address apparently has to be parsed from the worker thread name. 在JBoss 5之前,显然必须从辅助线程名称中解析IP地址。 And that seems to be the only way to do it on earlier versions. 这似乎是在早期版本中执行此操作的唯一方法。 This is the code snippet doing it (copied from the above link): 这是这样做的代码片段(从上面的链接复制):

private String getCurrentClientIpAddress() {
    String currentThreadName = Thread.currentThread().getName();
    System.out.println("Threadname: "+currentThreadName);
    int begin = currentThreadName.indexOf('[') +1;
    int end = currentThreadName.indexOf(']')-1;
    String remoteClient = currentThreadName.substring(begin, end);
    return remoteClient;
}

Have you tried: java.rmi.server.RemoteServer.getClientHost() ? 您是否尝试过:java.rmi.server.RemoteServer.getClientHost()?

http://java.sun.com/j2se/1.5.0/docs/api/java/rmi/server/RemoteServer.html#getClientHost() http://java.sun.com/j2se/1.5.0/docs/api/java/rmi/server/RemoteServer.html#getClientHost()

I believe that name of the current worker thread contains an IP address of the server, but not the client's IP since threads are pooled rather than created for each call. 我认为当前工作线程的名称包含服务器的IP地址,而不包含客户端的IP地址,因为线程是池化的,而不是为每个调用创建的。 In JBoss 4, one can use the following workaround to obtain an IP address of the client: 在JBoss 4中,可以使用以下变通办法来获取客户端的IP地址:

        try {
            //Reflection is used to avoid compile-time dependency on JBoss internal libraries
            Class clazz = Class.forName("org.jboss.web.tomcat.security.HttpServletRequestPolicyContextHandler");
            Field requestContextField = clazz.getDeclaredField("requestContext");
            requestContextField.setAccessible(true);
            ThreadLocal ctx = (ThreadLocal) requestContextField.get(null);
            ServletRequest req = ((ServletRequest) ctx.get());
            return req==null?null:req.getRemoteAddr();
        } catch (Exception e) {
            LOG.log(Level.WARNING, "Failed to determine client IP address",e);
        }

Thanks to MicSim, I learned that the thread name stores the IP address. 多亏了MicSim,我才知道线程名称存储了IP地址。 In JBoss 4.2.2 the thread name for EJB2 items look like this: 在JBoss 4.2.2中,EJB2项的线程名称如下所示:

http-127.0.0.1-8080-2 HTTP-127.0.0.1-8080-2

(I assume the http is optional, depending on the protocol actually used). (我认为http是可选的,取决于实际使用的协议)。

This can then be parsed with a regular expression as so: 然后可以使用正则表达式进行解析,如下所示:

    Pattern pattern = Pattern.compile("\\b(?:\\d{1,3}\\.){3}\\d{1,3}\\b");
    Matcher matcher = pattern.matcher(Thread.currentThread().getName());
    if (matcher.find()) {
        return matcher.group();
    }
    return "";

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

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