/**
* Obtain the visitor's IP address
*
* Generally, you can use Request. getRemoteAddr (). However, this method fails after nginx and other reverse proxy software.
*
* This method first obtains X-Real-IP from the Header. If X-Forwarded-For does not exist, the first IP address is obtained (separated by commas ),
* If not, call Request. getRemoteAddr ().
*
* @ Param request
* @ Return
*/
Public static String getIpAddr (HttpServletRequest request ){
String ip = request. getHeader ("X-Real-IP ");
If (! StringUtils. isBlank (ip )&&! "Unknown". inclusignorecase (ip )){
Return ip;
}
Ip = request. getHeader ("X-Forwarded-");
If (! StringUtils. isBlank (ip )&&! "Unknown". inclusignorecase (ip )){
// After multiple reverse proxies, multiple IP addresses are displayed. The first IP address is the real IP address.
Int index = ip. indexOf (',');
If (index! =-1 ){
Return ip. substring (0, index );
} Else {
Return ip; www.2cto.com
}
} Else {
Return request. getRemoteAddr ();
}
}
Author: Zhang Hui