Browse Source

6-13使用后置中间件处理登陆流

Home 5 years ago
parent
commit
26992a4543
3 changed files with 51 additions and 4 deletions
  1. 4 4
      app/admin/controller/AdminBase.php
  2. 11 0
      app/admin/middleware.php
  3. 36 0
      app/admin/middleware/Auth.php

+ 4 - 4
app/admin/controller/AdminBase.php

@@ -20,10 +20,10 @@ class AdminBase extends BaseController
     public function initialize()
     public function initialize()
     {
     {
         parent::initialize();
         parent::initialize();
-        // 判断是否登录
-        if (!$this->isLogin()) {
-            return $this->redirect(url('login/index'), 302);
-        }
+        // 判断是否登录  判断是否登陆 切换到 中间件Auth中
+//        if (!$this->isLogin()) {
+//            return $this->redirect(url('login/index'), 302);
+//        }
     }
     }
 
 
     /**
     /**

+ 11 - 0
app/admin/middleware.php

@@ -0,0 +1,11 @@
+<?php
+// 全局中间件定义文件
+return [
+    // 全局请求缓存
+    // \think\middleware\CheckRequestCache::class,
+    // 多语言加载
+    // \think\middleware\LoadLangPack::class,
+    // Session初始化
+    \think\middleware\SessionInit::class,
+    app\admin\middleware\Auth::class
+];

+ 36 - 0
app/admin/middleware/Auth.php

@@ -0,0 +1,36 @@
+<?php
+/**
+ * Created by PhpStorm
+ * User:林志杰
+ * Email:[email protected]
+ * Motto:纵有疾风起,人生不言弃!
+ * Time:2020/1/4 0:15
+ */
+
+declare (strict_types=1);
+
+namespace app\admin\middleware;
+
+
+class Auth
+{
+    public function handle($request, \Closure $next)
+    {
+        // 前置中间件
+        $response = $next($request);
+        if (empty(session(config('admin.session_admin'))) && $request->controller() !== 'Login') {
+            return redirect((string)url('login/index'));
+        }
+        return $response;
+        // 后置中间件
+    }
+
+    /**
+     * 中间件结束调度
+     * @param \think\Response $response
+     */
+    public function end(\think\Response $response)
+    {
+
+    }
+}