From 60d43bf359dafeda1d6da654a2566bf4f764a2ec Mon Sep 17 00:00:00 2001 From: luxiaotao1123 <t1341870251@163.com> Date: 星期五, 07 八月 2020 08:13:15 +0800 Subject: [PATCH] # --- src/main/java/com/zy/common/config/AdminInterceptor.java | 5 ++ src/main/java/com/zy/common/utils/IpTools.java | 94 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/zy/common/config/AdminInterceptor.java b/src/main/java/com/zy/common/config/AdminInterceptor.java index 80e61c5..b42a596 100644 --- a/src/main/java/com/zy/common/config/AdminInterceptor.java +++ b/src/main/java/com/zy/common/config/AdminInterceptor.java @@ -64,6 +64,11 @@ } } } + // 鐧藉悕鍗� +// if (IpTools.gainRealIp(request).equals("127.0.0.1")) { +// request.setAttribute("userId", 9527); +// return true; +// } // 璺ㄥ煙璁剧疆 // response.setHeader("Access-Control-Allow-Origin", "*"); HandlerMethod handlerMethod = (HandlerMethod) handler; diff --git a/src/main/java/com/zy/common/utils/IpTools.java b/src/main/java/com/zy/common/utils/IpTools.java new file mode 100644 index 0000000..30a2592 --- /dev/null +++ b/src/main/java/com/zy/common/utils/IpTools.java @@ -0,0 +1,94 @@ +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,鎺掗櫎浠g悊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浠g悊 + // 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涓嬩篃鍙互鑾峰彇鏈湴鐨刬p鍦板潃 + 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) { + // 鑾峰彇鐪熷疄鐨処p鍦板潃 + ipAddress = ip.getHostAddress(); + + } + } + } + + } + + } + // 瀵逛簬閫氳繃澶氫釜浠g悊鐨勬儏鍐碉紝绗竴涓狪P涓哄鎴风鐪熷疄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{},瑙f瀽寮傚父", ipAddress, e); + } + return ipAddress; + + } + +} -- Gitblit v1.9.1