(HttpServletRequest request)
| 126 | private static final String UNKNOWN = "unknown"; |
| 127 | |
| 128 | public static String getIp(HttpServletRequest request) { |
| 129 | String ip = request.getHeader("x-forwarded-for"); |
| 130 | if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) { |
| 131 | ip = request.getHeader("Proxy-Client-IP"); |
| 132 | } |
| 133 | if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) { |
| 134 | ip = request.getHeader("WL-Proxy-Client-IP"); |
| 135 | } |
| 136 | if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) { |
| 137 | ip = request.getRemoteAddr(); |
| 138 | } |
| 139 | String comma = ","; |
| 140 | String localhost = "127.0.0.1"; |
| 141 | if (ip.contains(comma)) { |
| 142 | ip = ip.split(",")[0]; |
| 143 | } |
| 144 | if (localhost.equals(ip)) { |
| 145 | // 获取本机真正的ip地址 |
| 146 | try { |
| 147 | ip = InetAddress.getLocalHost().getHostAddress(); |
| 148 | } catch (UnknownHostException e) { |
| 149 | log.error(e.getMessage(), e); |
| 150 | } |
| 151 | } |
| 152 | return ip; |
| 153 | } |
| 154 | |
| 155 | } |
| 156 |
nothing calls this directly
no outgoing calls
no test coverage detected