我已将我的EJB应用程序从5.0.1 JBOSS迁移到JBOSS EAP 7。 我想在我的EJB拦截器(或bean)中检测客户端的IP地址
String currentThreadName = Thread.currentThread().getName();
result: default task-16
代码不再有效。如何获取客户端的IP地址?
答案 0 :(得分:2)
您可以尝试远程连接和IP地址。我不确定它有多可靠,因为org.jboss.as.security-api
是一个已弃用的模块,可能会在将来的版本中删除。
之后尝试下面的内容:
容器拦截器:
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
import java.net.InetAddress;
import java.security.Principal;
import org.jboss.remoting3.Connection;
import org.jboss.remoting3.security.InetAddressPrincipal;
import org.jboss.as.security.remoting.RemotingContext;
public class ClientIpInterceptor {
@AroundInvoke
private Object iAmAround(final InvocationContext invocationContext) throws Exception {
InetAddress remoteAddr = null;
Connection connection = RemotingContext.getConnection();
for (Principal p : connection.getPrincipals()) {
if (p instanceof InetAddressPrincipal) {
remoteAddr = ((InetAddressPrincipal) p).getInetAddress();
break;
}
}
System.out.println("IP " + remoteAddr);
return invocationContext.proceed();
}
}
的JBoss-ejb3.xml:
<?xml version="1.0" encoding="UTF-8"?>
<jboss xmlns="http://www.jboss.com/xml/ns/javaee"
xmlns:jee="http://java.sun.com/xml/ns/javaee"
xmlns:ci ="urn:container-interceptors:1.0">
<jee:assembly-descriptor>
<ci:container-interceptors>
<jee:interceptor-binding>
<ejb-name>*</ejb-name>
<interceptor-class>ClientIpInterceptor</interceptor-class>
</jee:interceptor-binding>
</ci:container-interceptors>
</jee:assembly-descriptor>
</jboss>
的JBoss部署-structure.xml:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
<deployment>
<dependencies>
<module name="org.jboss.remoting3" />
<module name="org.jboss.as.security-api" />
</dependencies>
</deployment>
</jboss-deployment-structure>
答案 1 :(得分:-1)
关于JBoss社区维基的这篇文章[1]正好解决了你的问题。在JBoss 5之前,显然必须从工作线程名称解析IP地址。这似乎是在早期版本中实现它的唯一方法。
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;
}
[1] https://developer.jboss.org/wiki/HowtogettheClientipaddressinanEJB3Interceptor