在Worklight Adapter上获取客户端IP地址

时间:2015-04-22 18:34:11

标签: ibm-mobilefirst worklight-adapters

我们正在使用WL6.2开发混合应用程序。在我们的适配器中,我们有以下代码:

var request = WL.Server.getClientRequest(); 
var IPAddress = request.getHeader('X-Archieved-Client-IP');
if(IPAddress == null || IPAddress == undefined || IPAddress == "") {
    IPAddress = request.getHeader('X-Archieved-Client-IP');
}
if(IPAddress == null || IPAddress == undefined || IPAddress == "") {
    IPAddress = request.getRemoteAddr();
}
if(IPAddress == null || IPAddress == undefined || IPAddress == "") {
    //This will never happen, only for precaution.
    IPAddress = "192.168.1.1";
}

commonParams.originatingIp = (IPAddress)?IPAddress:request.getRemoteAddr();

我无法在适配器上获取客户端IP地址,我们的Web服务只能看到一个IP地址,即数据电源IP。在日志中,我将数据电源IP地址视为“orginatingIP”。这意味着"getRemoteAddr()"正在执行。

<OriginatingIP>DataPowerIP</OriginatingIP>

如果我只保留这部分代码:

var request = WL.Server.getClientRequest(); 
var IPAddress = request.getHeader('X-Archieved-Client-IP');
commonParams.originatingIp = IPAddress;

我的价值是“null”。

<OriginatingIP>null</OriginatingIP>

我也试过这个解决方案“IBM Worklight 6 - How would i get client IP address on adapter side”也没有用。

有什么建议吗?感谢

2 个答案:

答案 0 :(得分:2)

我将按照以下更新您的适配器

var request = WL.Server.getClientRequest(); 

var ipAddress = request.getHeader('x-client-ip');

switch(ipAddress){
  case "":
  case null:
  case undefined:
    //This will never happen, only for precaution.
    ipAddress = "192.168.1.1";
  break;
}

从这篇文章https://developer.ibm.com/answers/questions/10123/how-do-you-get-the-original-ip-doing-the-request-to-your-bluemix-app.html看来,您应该使用x-client-ip而不是X-Archieved-Client-IP

答案 1 :(得分:0)

这个主题对我有用。

what is the right way to get request's ip

尝试 request.getRemoteAddr()