JBOSS EAP 7

编程入门 行业动态 更新时间:2024-10-28 14:28:21
JBOSS EAP 7 - EJB来电IP(JBOSS EAP 7 - EJB Caller IP)

我已将EJB应用程序从5.0.1 JBOSS迁移到JBOSS EAP 7.我想在我的EJB拦截器(或bean中)中检测客户端的IP地址,

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

代码不再适用。 如何获取客户端的IP地址?

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?

最满意答案

您可以尝试获取远程连接和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>

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>

更多推荐

本文发布于:2023-07-15 11:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1112843.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:JBOSS   EAP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!