java

位置:IT落伍者 >> java >> 浏览文章

Java开发的WebService获取客户端IP地址


发布日期:2020年07月09日
 
Java开发的WebService获取客户端IP地址

基于jaxws开发的webservice获取客户端ip的方法【在代理类XXXDelegate添加如下代码】

import javaxannotationResource;

import javaxservlethttpHttpServletRequest;

import javaxxmlwsWebServiceContext;

import javaxxmlwshandlerMessageContext;

@Resource

private WebServiceContext wsContext;

private String getClientInfo(){

MessageContext mc = wsContextgetMessageContext();

HttpServletRequest request = (HttpServletRequest)(mcget(MessageContextSERVLET_REQUEST));

String remortAddress = requestgetRemoteAddr();

return (remortAddress);

}

基于axis发布的webservice获取客户端ip的方法

/**

* 获取客户端IP地址如调用方的IP以便检查权限

* 适用于axis发布的webservice

* @return

*/

public String getClientIpAxis() {

MessageContext mc = null;

HttpServletRequest request = null;

try {

mc = MessageContextgetCurrentMessageContext();

if (mc == null)

throw new Exception(无法获取到MessageContext);

request = (HttpServletRequest) mc

getProperty(HTTPConstantsMC_HTTP_SERVLETREQUEST);

Systemoutprintln(remote ip: + requestgetRemoteAddr());

} catch (Exception e) {

Systemoutprintln(egetMessage());

eprintStackTrace();

}

return requestgetRemoteAddr();

}

基于xfire发布的webservice获取客户端ip的方法

/**

* 获取客户端IP地址

* 适用于xfire发布的webservice

* @return

*/

public String getClientIpXfire() {

String ip = ;

try {

HttpServletRequest request = XFireServletControllergetRequest();

ip = requestgetRemoteAddr();

} catch (Exception e) {

Systemoutprintln(无法获取HttpServletRequest);

eprintStackTrace();

}

return ip;

}

上一篇:Java远程调用之Hessian简例

下一篇:Java Listener模式