如何获得客户';在JavaSE JAX-WS生成的Web服务端点中的主机IP?
我正在JavaSE应用程序中启动一个WebService,我想获取客户端的调用方IP。 可能吗 我编写了一个小测试用例:如何获得客户';在JavaSE JAX-WS生成的Web服务端点中的主机IP?,java,web-services,jax-ws,endpoint,Java,Web Services,Jax Ws,Endpoint,我正在JavaSE应用程序中启动一个WebService,我想获取客户端的调用方IP。 可能吗 我编写了一个小测试用例: import java.net.InetSocketAddress; import java.util.concurrent.*; import javax.jws.*; import javax.xml.ws.Endpoint; import com.sun.net.httpserver.*; @WebService public class TestWs { @W
import java.net.InetSocketAddress;
import java.util.concurrent.*;
import javax.jws.*;
import javax.xml.ws.Endpoint;
import com.sun.net.httpserver.*;
@WebService
public class TestWs {
@WebMethod public String testMethod(String param) {
String clientHostIp = ""; // how to obtain client's host ?
return "hello "+ clientHostIp;
}
public static void main(String[] args) throws Exception {
ExecutorService executorService = Executors.newFixedThreadPool(2);
HttpServer thttpserver = HttpServer.create(new InetSocketAddress("0.0.0.0", 2000),8);
final HttpServer httpserver = thttpserver;
httpserver.setExecutor(executorService);
httpserver.start();
HttpContext ctx = httpserver.createContext("/TestWs");
final Endpoint wsendpoint = Endpoint.create(new TestWs());
wsendpoint.publish(ctx);
}
}
帮助我解决问题,但它将我的代码与内部sun API绑定:(
请参阅注意此限制:如果您的应用程序服务器位于web或代理服务器后面,则IP很可能是该主机的IP,而不是实际的客户端。
import java.net.InetSocketAddress;
import java.util.concurrent.*;
import javax.annotation.Resource;
import javax.jws.*;
import javax.xml.ws.*;
import com.sun.net.httpserver.*;
import com.sun.xml.internal.ws.developer.JAXWSProperties;
@WebService
public class TestWs {
@Resource
private WebServiceContext wsc;
@WebMethod public String testMethod(String param) {
HttpExchange exchange = (HttpExchange) wsc.getMessageContext().get(JAXWSProperties.HTTP_EXCHANGE);
return "hello "+ exchange.getRemoteAddress().getHostString();
}
public static void main(String[] args) throws Exception {
ExecutorService executorService = Executors.newFixedThreadPool(2);
HttpServer thttpserver = HttpServer.create(new InetSocketAddress("0.0.0.0", 2000), 8);
final HttpServer httpserver = thttpserver;
httpserver.setExecutor(executorService);
httpserver.start();
HttpContext ctx = httpserver.createContext("/TestWs");
final Endpoint wsendpoint = Endpoint.create(new TestWs());
wsendpoint.publish(ctx);
}
}