JBOSS EAP 7 - EJB主叫IP
问题描述:
我已经迁移我的EJB应用程序从5.0.1 JBOSS到JBoss EAP 7 我想在我的EJB拦截器来检测客户端的IP地址(或豆)JBOSS EAP 7 - EJB主叫IP
String currentThreadName = Thread.currentThread().getName();
result: default task-16
代码不再有效。如何获取客户端的IP地址?
答
你可以尝试获得远程连接和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>
答
JBoss社区wiki上的这篇文章[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
+0
我的应用程序在EAP 7中工作。 – user1028269
Connection connection = RemotingContext.getConnection() user1028269
@ user1028269我在EAP7中检查这个解决方案并且工作,参见https://github.com/fedesierr/eap7-ejb-remote –
我错过了jboss-ejb3 .xml拦截器配置,谢谢。 – user1028269