lip 3 månader sedan
förälder
incheckning
1bf40e8fe4

+ 2 - 1
app/admin/middleware.php

@@ -2,5 +2,6 @@
 return [
     "locale",
     "checkAuth",
-    // "checkPermission"
+    // "checkPermission",
+    "CheckIp"
 ];

+ 22 - 0
app/admin/model/IpConfig.php

@@ -10,4 +10,26 @@ class IpConfig extends BaseModel
     protected $createTime = 'created_at';
     protected $updateTime = 'updated_at';
     
+    public static function getWhitelist(){
+        //使用缓存
+        $cache = cache('ip_whitelist');
+        if ($cache) {
+            return $cache;
+        }
+        $whitelist = self::where('type', 2)->where('status', 1)->column('ip');
+        //缓存白名单(1小时)
+        cache('ip_whitelist', $whitelist, 60 * 60);
+        return $whitelist;
+    }
+    public static function getBlacklist(){
+        //使用缓存
+        $cache = cache('ip_blacklist');
+        if ($cache) {
+            return $cache;
+        }
+        $blacklist = self::where('type', 1)->where('status', 1)->column('ip');
+        //缓存黑名单(1小时)
+        cache('ip_blacklist', $blacklist, 60 * 60);
+        return $blacklist;
+    }
 }

+ 2 - 1
app/common/middleware.php

@@ -1,4 +1,5 @@
 <?php
 return [
-"locale"
+"locale",
+"CheckIp"
 ];

+ 27 - 0
app/common/middleware/CheckIp.php

@@ -0,0 +1,27 @@
+<?php
+namespace app\common\middleware;
+
+use Exception;
+use app\admin\model\IpConfig;
+
+//验证Ip
+class CheckIp
+{
+    public function handle($request, \Closure $next)
+    {
+        try {
+            $ip = $request->ip();
+            $whitelist = IpConfig::getWhitelist();
+            if(!in_array($ip, $whitelist)){
+                $blacklist = IpConfig::getBlacklist();
+                if(in_array($ip, $blacklist)){
+                    return shutdown(lang('user.ipError'), 10002);
+                }
+            }
+        } catch (Exception $exception) {
+            return shutdown($errorMsgArr[$exception->getMessage()] ?? $exception->getMessage(), 10002);
+        }
+      
+        return $next($request);
+    }
+}

+ 2 - 1
app/enterprise/middleware.php

@@ -1,5 +1,6 @@
 <?php
 return [
     "locale",
-    "checkAuth"
+    "checkAuth",
+    "CheckIp"
 ];

+ 3 - 2
app/manage/middleware.php

@@ -1,6 +1,7 @@
 <?php
 return [
     "locale",
-"checkAuth",
-"manageAuth",
+    "checkAuth",
+    "manageAuth",
+    "CheckIp"
 ];