Reputation: 693
I have migrated my EJB application from 5.0.1 JBOSS to JBOSS EAP 7. I want to detect IP address of client in my EJB interceptor (or in bean)
String currentThreadName = Thread.currentThread().getName();
result: default task-16
Code does not works anymore. How to get IP address of client?
Upvotes: 1
Views: 456
Reputation: 5208
You can try getting remoting Connection and IP address. I'm not sure how reliable it is because org.jboss.as.security-api
is a deprecated module which may be removed in future versions.
After that try the folowing:
Container interceptor:
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-deployment-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>
Upvotes: 3
Reputation: 874
This article[1] on the JBoss community wiki addresses exactly your issue. Prior to JBoss 5 the IP address apparently has to be parsed from the worker thread name. And that seems to be the only way to do it on earlier versions.
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
Upvotes: -2