it r=(.) (ServletRequestAttributes RequestContextHolder getRequestAttributes ()). GetRequest ();
R. gutierrez etHeader String IP=(" X-ray forwarded - for ");
System. The out. Println (" getIpAddress (it) - X - Forwarded - For - String IP="+ IP);
If (IP==null | | IP. The length ()==0 | | "unknown". EqualsIgnoreCase (IP)) {
If (IP==null | | IP. The length ()==0 | | "unknown". EqualsIgnoreCase (IP)) {
R. gutierrez etHeader IP=(" Proxy - the Client - IP ");
System. The out. Println (" getIpAddress (it) - Proxy - the Client - IP - String IP="+ IP);
}
If (IP==null | | IP. The length ()==0 | | "unknown". EqualsIgnoreCase (IP)) {
R. gutierrez etHeader IP=(" WL - Proxy - the Client - IP ");
System. Out. Println (" getIpAddress (it) - WL - Proxy - the Client - IP - String IP="+ IP);
}
If (IP==null | | IP. The length ()==0 | | "unknown". EqualsIgnoreCase (IP)) {
R. gutierrez etHeader IP=(" HTTP_CLIENT_IP ");
System. The out. Println (" getIpAddress (it) - HTTP_CLIENT_IP - String IP="+ IP);
}
If (IP==null | | IP. The length ()==0 | | "unknown". EqualsIgnoreCase (IP)) {
R. gutierrez etHeader IP=(" HTTP_X_FORWARDED_FOR ");
System. The out. Println (" getIpAddress (it) - HTTP_X_FORWARDED_FOR - String IP="+ IP);
}
If (IP==null | | IP. The length ()==0 | | "unknown". EqualsIgnoreCase (IP)) {
R. gutierrez etRemoteAddr IP=();
System. The out. Println (" getIpAddress (it) - getRemoteAddr - String IP="+ IP);
}
} else if (IP) length () & gt; 15) {
String [] ips=IP. The split (", ");
For (int index=0; The index & lt; Ips. Length; Index++) {
String strIp=(String) ips [index];
if (! (" unknown ". EqualsIgnoreCase (strIp))) {
IP=strIp;
break;
}
}
}
This is I now use the code
But on the server only through getRemoteAddr take to the gateway address head inside take x - forwarded - for Null
This is print figure
CodePudding user response:
X - forwarded - for, etc. These are the head of a field, the general HTTP load nginx, agency, etc will be (and need to configure nginx) support this way, you of this function is a kind of comprehensive packaging, considering many situation, so you have to consider your running environment,And getRemoteAddr read value from TCP/IP protocol, which is a network system of values (by the way, if use nginx such load tools getRemoteAddr get down or nginx IP, so the function processing order is also very important),
CodePudding user response:
Need in nginx configurationProxy_set_header Host $Host;
Proxy_set_header X - Real - IP $remote_addr;
Proxy_set_header X - Forwarded - For $proxy_add_x_forwarded_for;
Proxy_set_header X - Forwarded - Proto $scheme;