2017-01-18 4 views
0

Я мигрировали мои приложения EJB из 5.0.1 JBOSS в JBOSS EAP 7. Я хочу, чтобы обнаружить IP-адрес клиента в моем EJB-перехватчика (или фасоли)JBOSS EAP 7 - EJB IP Caller

String currentThreadName = Thread.currentThread().getName(); 
result: default task-16 

Код больше не работает. Как получить IP-адрес клиента?

ответ

1

Вы можете попытаться получить удаленное взаимодействие соединения и 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> 
+0

Connection connection = RemotingContext.getConnection() << is null :( – user1028269

+0

@ user1028269 Я проверяю это решение в EAP7 и работает, см. Https://github.com/fedesierr/eap7-ejb-remote –

+0

Я пропустил jboss-ejb3 .xml конфигурации перехватчика, спасибо. – user1028269

-1

Эта статья [1] на вики сообщества JBoss касается именно вашей проблемы. До 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

Смежные вопросы