New file |
| | |
| | | package com.zy.common.utils; |
| | | |
| | | import com.core.common.Cools; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.net.Inet4Address; |
| | | import java.net.InetAddress; |
| | | import java.net.NetworkInterface; |
| | | import java.util.Enumeration; |
| | | |
| | | /** |
| | | * Created by vincent on 2020/8/6 |
| | | */ |
| | | @Slf4j |
| | | public class IpTools { |
| | | |
| | | public static String gainRealIp(HttpServletRequest request) { |
| | | String ipAddress = ""; |
| | | try { |
| | | if (request == null) { |
| | | return ipAddress; |
| | | } |
| | | |
| | | //排除本地测试 |
| | | if ("127.0.0.1".equals(request.getServerName()) || "localhost".equals(request.getServerName())) { |
| | | ipAddress = "127.0.0.1"; |
| | | return ipAddress; |
| | | } |
| | | |
| | | ipAddress = request.getRemoteAddr(); |
| | | if (Cools.isEmpty(ipAddress)) { |
| | | ipAddress = request.getRemoteHost(); |
| | | } else { |
| | | return ipAddress; |
| | | } |
| | | |
| | | if (!Cools.isEmpty(ipAddress)) { |
| | | return ipAddress; |
| | | } |
| | | |
| | | // 获取真实ip,排除代理ip |
| | | ipAddress = request.getHeader("Referer"); |
| | | |
| | | // ipAddress = this.getRequest().getRemoteAddr(); |
| | | ipAddress = request.getHeader("X-Forwarded-For"); |
| | | if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { |
| | | ipAddress = request.getHeader("Proxy-Client-IP"); |
| | | } |
| | | if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { |
| | | ipAddress = request.getHeader("WL-Proxy-Client-IP"); |
| | | } |
| | | if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { |
| | | // 更换niginx代理 |
| | | // ipAddress = request.getRemoteAddr(); |
| | | ipAddress = request.getHeader("X-Real-IP"); |
| | | if (ipAddress != null && (ipAddress.equals("" + "") || ipAddress.endsWith("0:0:0:0:0:0:0:1"))) { |
| | | // 根据网卡取本机配置的IP |
| | | |
| | | // linux下也可以获取本地的ip地址 |
| | | Enumeration<?> allNetInterfaces = NetworkInterface.getNetworkInterfaces(); |
| | | InetAddress ip; |
| | | while (allNetInterfaces.hasMoreElements()) { |
| | | NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement(); |
| | | Enumeration<InetAddress> addresses = netInterface.getInetAddresses(); |
| | | while (addresses.hasMoreElements()) { |
| | | ip = addresses.nextElement(); |
| | | if (ip instanceof Inet4Address) { |
| | | // 获取真实的Ip地址 |
| | | ipAddress = ip.getHostAddress(); |
| | | |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | } |
| | | // 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割"***.***.***.***".length()=15 |
| | | if (ipAddress != null && ipAddress.length() > 15) { |
| | | |
| | | if (ipAddress.indexOf(",") > 0) { |
| | | ipAddress = ipAddress.substring(0, ipAddress.indexOf(",")); |
| | | } |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | | log.warn("ip{},解析异常", ipAddress, e); |
| | | } |
| | | return ipAddress; |
| | | |
| | | } |
| | | |
| | | } |