获取IP地址 @param request 请求信息 @return
(HttpServletRequest request)
| 56 | * @return |
| 57 | */ |
| 58 | public static String getClientIpAddress(HttpServletRequest request){ |
| 59 | String ip=request.getHeader("X-Forwarded-For"); |
| 60 | if(ip==null || ip.isEmpty() ||"unknown".equalsIgnoreCase(ip)){ |
| 61 | ip=request.getHeader("Proxy-Client-IP"); |
| 62 | } |
| 63 | if(ip==null || ip.isEmpty() ||"unknown".equalsIgnoreCase(ip)){ |
| 64 | ip=request.getHeader("WL-Proxy-Client-IP"); |
| 65 | } |
| 66 | if(ip==null || ip.isEmpty() ||"unknown".equalsIgnoreCase(ip)){ |
| 67 | ip=request.getRemoteAddr(); |
| 68 | } |
| 69 | //对于通过多个代理的情况, 第一个IP为客户端真实IP,多个IP按照','分割 "***.***.***.***".length() |
| 70 | if (ip != null && ip.length() > 15) { // "***.***.***.***".length() |
| 71 | // = 15 |
| 72 | if (ip.indexOf(",") > 0) { |
| 73 | ip = ip.substring(0, ip.indexOf(",")); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return ip; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | private static final String[] HEADERS_TO_TRY = { |
no test coverage detected