java 获取客户端真实IP地址


1. 在bs架构中,我们常常需要在日志中获取登录的IP和操作的IP地址。

经常我们的代码如下:

String ip = request.getHeader("X-Forwarded-For");
logger.debug("获取到客户端的ip地址为:" + ip);
if (ip != null) {
if (!ip.isEmpty() && !"unKnown".equalsIgnoreCase(ip)) {
int index = ip.indexOf(",");
if (index != -1) {
return ip.substring(0, index);
} else {
return ip;
}
}
}
ip = request.getHeader("X-Real-IP");
logger.debug("获取到客户端的ip地址为:" + ip);
if (ip != null) {
if (!ip.isEmpty() && !"unKnown".equalsIgnoreCase(ip)) {
return ip;
}
}
ip = request.getHeader("Proxy-Client-IP");
logger.debug("获取到客户端的ip地址为:" + ip);
if (ip != null) {
if (!ip.isEmpty() && !"unKnown".equalsIgnoreCase(ip)) {
return ip;
}
}
ip = request.getHeader("WL-Proxy-Client-IP");
logger.debug("获取到客户端的ip地址为:" + ip);
if (ip != null) {
if (!ip.isEmpty() && !"unKnown".equalsIgnoreCase(ip)) {
return ip;
}
}
ip = request.getRemoteAddr();
logger.debug("获取到客户端的ip地址为:" + ip);
return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;

前端Vue,后端springboot,在本地启动后测试发现没有问题。

重点是当部署到nginx上面出现了两种情况:

1. 在正向代理的情况下:

即 将打包好的内容放到HTML下面启动即可。这样的情况下 发现取到所有的IP 都是null。

2. 在反向代理的情况下:

server {
listen 8888;
server_name localhost;
client_max_body_size 200m;
#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root /usr/local/nginx/dist;
try_files $uri $uri/ @router;
index index.html index.htm;

}


location /prod-api {
rewrite ^.+prod-api/?(.*)$ /$1 break;
include uwsgi_params;
proxy_pass http://127.0.0.1:8080;
}
location @router {
rewrite ^.*$ /index.html last;
}

#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location @router { rewrite ^.*$ /index.html last;
}

}

nginx 配置信息如上。这样获取到所有的IP都为null。

解决方案:

1. 正向代理下用如下代码解决:

InetAddress byName = null;
try {
byName = getByName(request.getRemoteHost());
} catch (UnknownHostException e) {
logger.error(e.getMessage(), e);
}
logger.debug("获取到客户端的ip地址为:" + byName.getHostAddress());
System.out.println(byName.getHostAddress());

2. 反向代理的情况下要在NGINX下配置:

location /prod-api {
rewrite ^.+prod-api/?(.*)$ /$1 break;
include uwsgi_params;
proxy_pass http://127.0.0.1:8080;

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

代码用

String ip = request.getHeader("X-Forwarded-For");
logger.debug("获取到客户端的ip地址为:" + ip);
if (ip != null) {
if (!ip.isEmpty() && !"unKnown".equalsIgnoreCase(ip)) {
int index = ip.indexOf(",");
if (index != -1) {
return ip.substring(0, index);
} else {
return ip;
}
}
}
ip = request.getHeader("X-Real-IP");
logger.debug("获取到客户端的ip地址为:" + ip);
if (ip != null) {
if (!ip.isEmpty() && !"unKnown".equalsIgnoreCase(ip)) {
return ip;
}
}
ip = request.getHeader("Proxy-Client-IP");
logger.debug("获取到客户端的ip地址为:" + ip);
if (ip != null) {
if (!ip.isEmpty() && !"unKnown".equalsIgnoreCase(ip)) {
return ip;
}
}
ip = request.getHeader("WL-Proxy-Client-IP");
logger.debug("获取到客户端的ip地址为:" + ip);
if (ip != null) {
if (!ip.isEmpty() && !"unKnown".equalsIgnoreCase(ip)) {
return ip;
}
}
ip = request.getRemoteAddr();
logger.debug("获取到客户端的ip地址为:" + ip);
return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;

总结:用了nginx 之后获取客户端IP有好多种办法。大家一定要注意。

            

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM