2013-07-08 36 views
0

我正在开发REST API,在某些情况下,我想确保请求来自设置在后端UI中的已知IP。 我尝试这样做:验证请求主机

try { 
    URL url = new URL(allowedHostname); 
    InetAddress[] allowedIps = InetAddress.getAllByName(url.getHost()); 
    for (InetAddress host : allowedIps) { 
     if (requesterIp.equals(host.getHostAddress())) { 
      return true; 
     } 
    } 
} catch (UnknownHostException e) { 
    logger.warn("[validateHostname] ", e); 
} 
return false; 

其中allowedHostname = request.getRemoteAddr()

但它似乎并不工作。 我不想只验证主机名,因为以另一个主机的名义发出请求相对容易。

编辑

requesterIp = request.getRemoteAddr()

allowedHostname =预定义的URL在后台UI设置

回答