java jax ws 获取客户端 IP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12727989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:59:18  来源:igfitidea点击:

jax ws getting client ip

javaweb-servicesjax-ws

提问by bazic

I'm trying to retrieve the client IP with JAX-WS, I used:

我正在尝试使用 JAX-WS 检索客户端 IP,我使用了:

@Resource
WebServiceContext wsContext; 

MessageContext mc = wsContext.getMessageContext();
HttpServletRequest req = (HttpServletRequest)mc.get(MessageContext.SERVLET_REQUEST); 

log.info("["+req.getRemoteAddr()+"])

I get a NullPointerExceptionin req, mc is not null.

我得到一个NullPointerException请求,mc 不为空。

My question is which JAR to use for HttpServletRequestbecause I'm using a Java stand-alone application?

我的问题是使用哪个 JAR,HttpServletRequest因为我使用的是 Java 独立应用程序?

Thanks

谢谢

回答by Moa

How to get the webservice client address for a jax-ws service depends on whether you are:

如何获取 jax-ws 服务的 webservice 客户端地址取决于您是否:

  • Running your webservice as a servlet (in a Java EE container), or
  • Running your webservice as a stand-alone application (Java SE 6 or 7).
  • 将您的网络服务作为 servlet(在 Java EE 容器中)运行,或
  • 将您的 Web 服务作为独立应用程序(Java SE 6 或 7)运行。

Servlet WebservicesIf your webservice is a servlet then use the solution of the first post that contains the following:

Servlet Webservices如果您的 Web 服务是一个 servlet,则使用包含以下内容的第一篇文章的解决方案:

HttpServletRequest req = (HttpServletRequest)mc.get(MessageContext.SERVLET_REQUEST);

Application Webservices : JAX-WS 2.1If you are using a Java application (Java SE) you have no servlet context, so the HttpServletRequestwill be null. You need to use the method of the later post, the one that has the following line:

应用程序 Web 服务:JAX-WS 2.1如果您使用的是 Java 应用程序 (Java SE),则您没有 servlet 上下文,因此HttpServletRequest将为空。您需要使用后一篇文章的方法,即具有以下行的方法:

HttpExchange exchange = (HttpExchange)msgx.get(JAXWSProperties.HTTP_EXCHANGE);

Note: this only works with the JAX-WS 2.1 stack/reference implementation.

注意:这仅适用于 JAX-WS 2.1 堆栈/参考实现。

Application Webservices : JAX-WS 2.2

应用程序 Web 服务:JAX-WS 2.2

In JAX-WS 2.2 the value of JAXWSProperties.HTTP_EXCHANGEhas changed from "com.sun.xml.ws.http.exchange" (the value it was in JAX-WS 2.1) to "com.sun.xml.internal.ws.http.exchange". That means that a call to

在 JAX-WS 2.2 中, 的值JAXWSProperties.HTTP_EXCHANGE已从“com.sun.xml.ws.http.exchange”(它在 JAX-WS 2.1 中的值)更改为“com.sun.xml.internal.ws.http.exchange” ”。这意味着调用

HttpExchange exchange = (HttpExchange)msgx.get(JAXWSProperties.HTTP_EXCHANGE);
InetSocketAddress remoteAddress = exchange.getRemoteAddress();
String remoteHost = remoteAddress.getHostName();

will return null in JAX-WS 2.2 and you'll get a NullPointerExceptionon the second line, and more importantly, cannot get the remote address of the client.

将在 JAX-WS 2.2 中返回 null 并且您将NullPointerException在第二行得到一个,更重要的是,无法获取客户端的远程地址。

If you use the following call instead (using the literal value, ugh!):

如果您改用以下调用(使用文字值,呃!):

HttpExchange exchange = (HttpExchange)msgx.get("com.sun.xml.ws.http.exchange");
InetSocketAddress remoteAddress = exchange.getRemoteAddress();
String remoteHost = remoteAddress.getHostName();

you will get a non-null value and will be able to obtain the client address.

您将获得一个非空值,并且将能够获得客户端地址。

So, how you get the remote address of the client depends on how you deploy your code (servlet or application) and which version of JAX-WS you are using (JAX-WS 2.1 or 2.2).

因此,如何获取客户端的远程地址取决于您如何部署代码(servlet 或应用程序)以及您使用的是哪个版本的 JAX-WS(JAX-WS 2.1 或 2.2)。

Recommendations

建议

  • Servlets: If you are deploying your JAX-WS webservice in a servlet you can always use the call to get the property MessageContext.SERVLET_REQUESTno matter what version of JAX-WS 2 you are using.

  • Applications: If you are deploying your JAX-WS webservice in an application you can always use the call HttpExchange exchange = (HttpExchange)msgx.get("com.sun.xml.ws.http.exchange");no matter whether you are using JAX-WS 2.1 or 2.2, therefore it is probably better to use the string literal in your code rather than the symbolic JAXWSProperties.HTTP_EXCHANGE.

  • Servlets:如果您在 servlet 中部署 JAX-WS Web 服务,MessageContext.SERVLET_REQUEST无论您使用什么版本的 JAX-WS 2,您都可以始终使用调用来获取属性。

  • 应用程序:如果您在应用程序中部署 JAX-WS Web 服务,HttpExchange exchange = (HttpExchange)msgx.get("com.sun.xml.ws.http.exchange");无论您使用的是 JAX-WS 2.1 还是 2.2,您都可以始终使用该调用,因此在代码中使用字符串文字可能比符号更好JAXWSProperties.HTTP_EXCHANGE.

As distasteful as using the literal is, it is better to have more robust code that works across JAX-WS versions rather than prettier code that doesn't.

与使用字面量一样令人反感的是,最好有更健壮的代码可以跨 JAX-WS 版本运行,而不是更漂亮的代码。

I hope the JAX-WS team correct the issue sometime and restore the value of JAXWSProperties.HTTP_EXCHANGEto the useful value again.

我希望 JAX-WS 团队有时间纠正这个问题,并JAXWSProperties.HTTP_EXCHANGE再次将 的值恢复到有用的值。

Many thanks to the earlier posters that showed the various ways of finding the remote address of JAX-WS clients. The information was very useful :)

非常感谢早期的海报展示了查找 JAX-WS 客户端远程地址的各种方法。这些信息非常有用:)

回答by bazic

if we use an embedded Http server , we can get the client IP like this:

如果我们使用嵌入式 Http 服务器,我们可以像这样获取客户端 IP:

@Resource
WebServiceContext wsContext;

Then in the web method :

然后在网络方法中:

@WebMethod
MessageContext msgx = wsContext.getMessageContext();
HttpExchange exchange = (HttpExchange)msgx.get(JAXWSProperties.HTTP_EXCHANGE);
log.info("["+exchange.getRemoteAddress().getAddress()+"])

Hope it helps someone else

希望它能帮助别人

回答by sgpalit

Here is an answer, I think you should initialize mc and req in WebMethod annotated method. Geting the IP Address Of A client For a webservice

这是一个答案,我认为您应该在 WebMethod 注释方法中初始化 mc 和 req 。 获取 Web 服务的客户端 IP 地址

回答by Raj Raman

The following code might work:

以下代码可能有效:

HttpServletRequest request = (HttpServletRequest) messageContext.get("transport.http.servletRequest");
String hostIp = request.getRemoteAddr();