Java????????IP
???????????? ???????[ 2015/12/1 10:47:55 ] ??????????????? ???????????
??????????????У????????????????????IP?????????????IP??????????:request.getRemoteAddr();???????????Apache??Squid????????????????????????????IP??????
???????:????????????????????????м?????????????????????????????IP????????????????????????????????????????????
????????????????????IP??????
???????????????????????????????????????????IP
????????????????????????????:Nginx??Squid?????????????????????????:
?????????????X-Forwarded-For??X-Real-IP??????http?????Э?????????squid????????????????????????resin????????????NGINX???????????????????????http????
????location / {
????proxy_pass http://yourdomain.com;
????proxy_set_header Host $host;
????proxy_set_header X-Real-IP $remote_addr;
????proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
????}????X-Forwarded-For???????????ips[0]???????????ip?????????????????????????????????????????????????????????????????????????????????????????????????·????????????治???????????????????ip??????????????;
????request.getRemoteAddr() 192.168.239.196
????request.getHeader("X-Forwarded-For") 58.63.227.162?? 192.168.237.178?? 192.168.238.218
????request.getHeader("X-Real-IP") 192.168.238.218
????????????????????????????????58.63.227.162????????????192.168.237.178?? 192.168.238.218?????????????192.168.239.196???NGINX???NGINX??X-Real-IP???????????????remote_addr?????????????????192.168.238.218???????resin????????????resin?????????????remote_addr??NGINX??ip?????192.168.239.196?????resin?????request.getRemoteAddr()??192.168.239.196???????resin????????ip??????????????????????ip?????????????
???????XFF????????XFF?????????
???????XFF??????XRI
???????XRI?????????request.getRemoteAddr()????????????????????????ip???
????????????????????ο????????£?
????????????
/**
* ??Request?????л??????IP????????HTTP???????????Nginx????????????ip
* @param request
* @return ip
*/
public static String getLocalIp(HttpServletRequest request) {
String remoteAddr = request.getRemoteAddr();
String forwarded = request.getHeader("X-Forwarded-For");
String realIp = request.getHeader("X-Real-IP");
String ip = null;
if (realIp == null) {
if (forwarded == null) {
ip = remoteAddr;
} else {
ip = remoteAddr + "/" + forwarded.split("??")[0];
}
} else {
if (realIp.equals(forwarded)) {
ip = realIp;
} else {
if(forwarded != null){
forwarded = forwarded.split("??")[0];
}
ip = realIp + "/" + forwarded;
}
}
return ip;
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11