jax ws 获取客户端 IP [英] jax ws getting client ip

查看:37
本文介绍了jax ws 获取客户端 IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

@Resource
WebServiceContext wsContext; 

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

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

我在 req 中得到一个 NullPointerException,mc 不为空.

I get a NullPointerException in req, mc is not null.

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

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

谢谢

推荐答案

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

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

  • 将您的网络服务作为 servlet(在 Java EE 容器中)运行,或
  • 将您的网络服务作为独立应用程序(Java SE 6 或 7)运行.

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

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

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

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

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

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

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

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

应用网络服务:JAX-WS 2.2

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

In JAX-WS 2.2 the value of JAXWSProperties.HTTP_EXCHANGE has 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

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

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

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

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

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.

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

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).

建议

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

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

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

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.

尽管使用文字令人反感,但最好是拥有更健壮的代码,可以跨 JAX-WS 版本运行,而不是更漂亮的代码,而不是不运行的代码.

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 团队有时间纠正这个问题,并再次将 JAXWSProperties.HTTP_EXCHANGE 的值恢复到有用的值.

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

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

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 获取客户端 IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆