如何取客户端真实的IP地址
目前客户的应用发布在内网,但是可以通过外网进行访问,但是为了安全,客户要求区分用户是在外网还是内网登陆的,
所以需要取到登陆人所在客户端的IP,通过和内网ip段做比较还区分是内外网登陆。现在我用了request.getRemoteAddr();和
request.getHeader("x-forwarded-for")这两种方法都没法得到正确的客户端ip,求高手指点。
------解决方案--------------------你用的什么系统啊?
windows 7?
------解决方案--------------------
//获得ip地址
private String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
不知道这个可不可以,我之前收集到的方法..LZ测试一下..
------解决方案--------------------如果是windows
Process process = Runtime.getRuntime().exec("ipconfig /all"); //windows下命令,
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
然后把输出中的ip取出来
如果是linux
Process process = Runtime.getRuntime().exec("ifconfig -a"); //linux下的命令
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));