Browse Source

访问控制需要排除本机docker

wukai 1 year ago
parent
commit
48838bd9df

+ 73 - 125
doc-common/src/main/java/com/doc/common/utils/ip/IpUtils.java

@@ -9,60 +9,58 @@ import java.net.UnknownHostException;
 
 /**
  * 获取IP方法
- * 
+ *
  * @author ruoyi
  */
-public class IpUtils
-{
+public class IpUtils {
     public final static String REGX_0_255 = "(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)";
-    // 匹配 ip
+    /**
+     * 匹配IP
+     */
     public final static String REGX_IP = "((" + REGX_0_255 + "\\.){3}" + REGX_0_255 + ")";
+    /**
+     * 匹配通配符
+     */
     public final static String REGX_IP_WILDCARD = "(((\\*\\.){3}\\*)|(" + REGX_0_255 + "(\\.\\*){3})|(" + REGX_0_255 + "\\." + REGX_0_255 + ")(\\.\\*){2}" + "|((" + REGX_0_255 + "\\.){3}\\*))";
-    // 匹配网段
+    /**
+     * 匹配网段
+     */
     public final static String REGX_IP_SEG = "(" + REGX_IP + "\\-" + REGX_IP + ")";
 
     /**
      * 获取客户端IP
-     * 
+     *
      * @return IP地址
      */
-    public static String getIpAddr()
-    {
+    public static String getIpAddr() {
         return getIpAddr(ServletUtils.getRequest());
     }
 
     /**
      * 获取客户端IP
-     * 
+     *
      * @param request 请求对象
      * @return IP地址
      */
-    public static String getIpAddr(HttpServletRequest request)
-    {
-        if (request == null)
-        {
+    public static String getIpAddr(HttpServletRequest request) {
+        if (request == null) {
             return "unknown";
         }
         String ip = request.getHeader("x-forwarded-for");
-        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
-        {
+        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
             ip = request.getHeader("Proxy-Client-IP");
         }
-        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
-        {
+        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
             ip = request.getHeader("X-Forwarded-For");
         }
-        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
-        {
+        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
             ip = request.getHeader("WL-Proxy-Client-IP");
         }
-        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
-        {
+        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
             ip = request.getHeader("X-Real-IP");
         }
 
-        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
-        {
+        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
             ip = request.getRemoteAddr();
         }
 
@@ -71,26 +69,23 @@ public class IpUtils
 
     /**
      * 检查是否为内部IP地址
-     * 
+     *
      * @param ip IP地址
      * @return 结果
      */
-    public static boolean internalIp(String ip)
-    {
+    public static boolean internalIp(String ip) {
         byte[] addr = textToNumericFormatV4(ip);
         return internalIp(addr) || "127.0.0.1".equals(ip);
     }
 
     /**
      * 检查是否为内部IP地址
-     * 
+     *
      * @param addr byte地址
      * @return 结果
      */
-    private static boolean internalIp(byte[] addr)
-    {
-        if (StringUtils.isNull(addr) || addr.length < 2)
-        {
+    private static boolean internalIp(byte[] addr) {
+        if (StringUtils.isNull(addr) || addr.length < 2) {
             return true;
         }
         final byte b0 = addr[0];
@@ -104,18 +99,15 @@ public class IpUtils
         // 192.168.x.x/16
         final byte SECTION_5 = (byte) 0xC0;
         final byte SECTION_6 = (byte) 0xA8;
-        switch (b0)
-        {
+        switch (b0) {
             case SECTION_1:
                 return true;
             case SECTION_2:
-                if (b1 >= SECTION_3 && b1 <= SECTION_4)
-                {
+                if (b1 >= SECTION_3 && b1 <= SECTION_4) {
                     return true;
                 }
             case SECTION_5:
-                switch (b1)
-                {
+                switch (b1) {
                     case SECTION_6:
                         return true;
                 }
@@ -126,29 +118,24 @@ public class IpUtils
 
     /**
      * 将IPv4地址转换成字节
-     * 
+     *
      * @param text IPv4地址
      * @return byte 字节
      */
-    public static byte[] textToNumericFormatV4(String text)
-    {
-        if (text.length() == 0)
-        {
+    public static byte[] textToNumericFormatV4(String text) {
+        if (text.length() == 0) {
             return null;
         }
 
         byte[] bytes = new byte[4];
         String[] elements = text.split("\\.", -1);
-        try
-        {
+        try {
             long l;
             int i;
-            switch (elements.length)
-            {
+            switch (elements.length) {
                 case 1:
                     l = Long.parseLong(elements[0]);
-                    if ((l < 0L) || (l > 4294967295L))
-                    {
+                    if ((l < 0L) || (l > 4294967295L)) {
                         return null;
                     }
                     bytes[0] = (byte) (int) (l >> 24 & 0xFF);
@@ -158,14 +145,12 @@ public class IpUtils
                     break;
                 case 2:
                     l = Integer.parseInt(elements[0]);
-                    if ((l < 0L) || (l > 255L))
-                    {
+                    if ((l < 0L) || (l > 255L)) {
                         return null;
                     }
                     bytes[0] = (byte) (int) (l & 0xFF);
                     l = Integer.parseInt(elements[1]);
-                    if ((l < 0L) || (l > 16777215L))
-                    {
+                    if ((l < 0L) || (l > 16777215L)) {
                         return null;
                     }
                     bytes[1] = (byte) (int) (l >> 16 & 0xFF);
@@ -173,29 +158,24 @@ public class IpUtils
                     bytes[3] = (byte) (int) (l & 0xFF);
                     break;
                 case 3:
-                    for (i = 0; i < 2; ++i)
-                    {
+                    for (i = 0; i < 2; ++i) {
                         l = Integer.parseInt(elements[i]);
-                        if ((l < 0L) || (l > 255L))
-                        {
+                        if ((l < 0L) || (l > 255L)) {
                             return null;
                         }
                         bytes[i] = (byte) (int) (l & 0xFF);
                     }
                     l = Integer.parseInt(elements[2]);
-                    if ((l < 0L) || (l > 65535L))
-                    {
+                    if ((l < 0L) || (l > 65535L)) {
                         return null;
                     }
                     bytes[2] = (byte) (int) (l >> 8 & 0xFF);
                     bytes[3] = (byte) (int) (l & 0xFF);
                     break;
                 case 4:
-                    for (i = 0; i < 4; ++i)
-                    {
+                    for (i = 0; i < 4; ++i) {
                         l = Integer.parseInt(elements[i]);
-                        if ((l < 0L) || (l > 255L))
-                        {
+                        if ((l < 0L) || (l > 255L)) {
                             return null;
                         }
                         bytes[i] = (byte) (int) (l & 0xFF);
@@ -204,9 +184,7 @@ public class IpUtils
                 default:
                     return null;
             }
-        }
-        catch (NumberFormatException e)
-        {
+        } catch (NumberFormatException e) {
             return null;
         }
         return bytes;
@@ -214,34 +192,26 @@ public class IpUtils
 
     /**
      * 获取IP地址
-     * 
+     *
      * @return 本地IP地址
      */
-    public static String getHostIp()
-    {
-        try
-        {
+    public static String getHostIp() {
+        try {
             return InetAddress.getLocalHost().getHostAddress();
-        }
-        catch (UnknownHostException e)
-        {
+        } catch (UnknownHostException e) {
         }
         return "127.0.0.1";
     }
 
     /**
      * 获取主机名
-     * 
+     *
      * @return 本地主机名
      */
-    public static String getHostName()
-    {
-        try
-        {
+    public static String getHostName() {
+        try {
             return InetAddress.getLocalHost().getHostName();
-        }
-        catch (UnknownHostException e)
-        {
+        } catch (UnknownHostException e) {
         }
         return "未知";
     }
@@ -252,16 +222,12 @@ public class IpUtils
      * @param ip 获得的IP地址
      * @return 第一个非unknown IP地址
      */
-    public static String getMultistageReverseProxyIp(String ip)
-    {
+    public static String getMultistageReverseProxyIp(String ip) {
         // 多级反向代理检测
-        if (ip != null && ip.indexOf(",") > 0)
-        {
+        if (ip != null && ip.indexOf(",") > 0) {
             final String[] ips = ip.trim().split(",");
-            for (String subIp : ips)
-            {
-                if (false == isUnknown(subIp))
-                {
+            for (String subIp : ips) {
+                if (false == isUnknown(subIp)) {
                     ip = subIp;
                     break;
                 }
@@ -276,39 +242,33 @@ public class IpUtils
      * @param checkString 被检测的字符串
      * @return 是否未知
      */
-    public static boolean isUnknown(String checkString)
-    {
+    public static boolean isUnknown(String checkString) {
         return StringUtils.isBlank(checkString) || "unknown".equalsIgnoreCase(checkString);
     }
 
     /**
      * 是否为IP
      */
-    public static boolean isIP(String ip)
-    {
+    public static boolean isIP(String ip) {
         return StringUtils.isNotBlank(ip) && ip.matches(REGX_IP);
     }
 
     /**
      * 是否为IP,或 *为间隔的通配符地址
      */
-    public static boolean isIpWildCard(String ip)
-    {
+    public static boolean isIpWildCard(String ip) {
         return StringUtils.isNotBlank(ip) && ip.matches(REGX_IP_WILDCARD);
     }
 
     /**
      * 检测参数是否在ip通配符里
      */
-    public static boolean ipIsInWildCardNoCheck(String ipWildCard, String ip)
-    {
+    public static boolean ipIsInWildCardNoCheck(String ipWildCard, String ip) {
         String[] s1 = ipWildCard.split("\\.");
         String[] s2 = ip.split("\\.");
         boolean isMatchedSeg = true;
-        for (int i = 0; i < s1.length && !s1[i].equals("*"); i++)
-        {
-            if (!s1[i].equals(s2[i]))
-            {
+        for (int i = 0; i < s1.length && !s1[i].equals("*"); i++) {
+            if (!s1[i].equals(s2[i])) {
                 isMatchedSeg = false;
                 break;
             }
@@ -319,29 +279,25 @@ public class IpUtils
     /**
      * 是否为特定格式如:“10.10.10.1-10.10.10.99”的ip段字符串
      */
-    public static boolean isIPSegment(String ipSeg)
-    {
+    public static boolean isIPSegment(String ipSeg) {
         return StringUtils.isNotBlank(ipSeg) && ipSeg.matches(REGX_IP_SEG);
     }
 
     /**
      * 判断ip是否在指定网段中
      */
-    public static boolean ipIsInNetNoCheck(String iparea, String ip)
-    {
+    public static boolean ipIsInNetNoCheck(String iparea, String ip) {
         int idx = iparea.indexOf('-');
         String[] sips = iparea.substring(0, idx).split("\\.");
         String[] sipe = iparea.substring(idx + 1).split("\\.");
         String[] sipt = ip.split("\\.");
         long ips = 0L, ipe = 0L, ipt = 0L;
-        for (int i = 0; i < 4; ++i)
-        {
+        for (int i = 0; i < 4; ++i) {
             ips = ips << 8 | Integer.parseInt(sips[i]);
             ipe = ipe << 8 | Integer.parseInt(sipe[i]);
             ipt = ipt << 8 | Integer.parseInt(sipt[i]);
         }
-        if (ips > ipe)
-        {
+        if (ips > ipe) {
             long t = ips;
             ips = ipe;
             ipe = t;
@@ -351,30 +307,22 @@ public class IpUtils
 
     /**
      * 校验ip是否符合过滤串规则
-     * 
+     *
      * @param filter 过滤IP列表,支持后缀'*'通配,支持网段如:`10.10.10.1-10.10.10.99`
-     * @param ip 校验IP地址
+     * @param ip     校验IP地址
      * @return boolean 结果
      */
-    public static boolean isMatchedIp(String filter, String ip)
-    {
-        if (StringUtils.isEmpty(filter) || StringUtils.isEmpty(ip))
-        {
+    public static boolean isMatchedIp(String filter, String ip) {
+        if (StringUtils.isEmpty(filter) || StringUtils.isEmpty(ip)) {
             return false;
         }
         String[] ips = filter.split(";");
-        for (String iStr : ips)
-        {
-            if (isIP(iStr) && iStr.equals(ip))
-            {
+        for (String iStr : ips) {
+            if (isIP(iStr) && iStr.equals(ip)) {
                 return true;
-            }
-            else if (isIpWildCard(iStr) && ipIsInWildCardNoCheck(iStr, ip))
-            {
+            } else if (isIpWildCard(iStr) && ipIsInWildCardNoCheck(iStr, ip)) {
                 return true;
-            }
-            else if (isIPSegment(iStr) && ipIsInNetNoCheck(iStr, ip))
-            {
+            } else if (isIPSegment(iStr) && ipIsInNetNoCheck(iStr, ip)) {
                 return true;
             }
         }

+ 3 - 0
doc-framework/src/main/java/com/doc/framework/interceptor/WhiteListInterceptor.java

@@ -31,6 +31,9 @@ public class WhiteListInterceptor implements HandlerInterceptor {
         if (StringUtils.isEmpty(whiteList) || no.equals(whiteList)) {
             return true;
         }
+        System.err.println(IpUtils.getIpAddr());
+        //需要排除本机docker的访问
+        whiteList += ";172.17.0.*";
         if (IpUtils.isMatchedIp(whiteList, IpUtils.getIpAddr())) {
             return true;
         } else {