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

查看:284
本文介绍了jax获取客户端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.

我的问题是哪个JAR到用于 HttpServletRequest 因为我使用的是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:


  • 将您的Web服务作为servlet运行(在Java EE容器中),或

  • 将Web服务作为独立应用程序运行(Java SE 6或7)。

Servlet Webservices
如果您的webservice是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);

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

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.

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

在JAX-WS 2.2中,<$ c的值$ c> 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 Web服务,可以始终使用该调用来获取属性 MessageContext.SERVLET_REQUEST no无论您使用的是哪种版本的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 Web服务应用程序你总是可以使用调用 HttpExchange exchange =(HttpExchange)msgx.get(com.sun.xml.ws.http.exchange); 无论你是否使用JAX-WS 2.1或2.2,因此最好在代码中使用字符串文字而不是符号 J AXWSProperties.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 <的值/ code>再次使用有用的值。

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

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