From 5182111a65e852c8adc6658535e21bbfce80cb48 Mon Sep 17 00:00:00 2001 From: win7 Date: Mon, 2 Dec 2019 17:00:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=AD=E9=97=B4=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/BaseController.php | 7 ++++- app/middleware/Auth.php | 38 +++++++++++++++++++++++++ app/middleware/UserLogin.php | 44 +++++++++++++++++++++++++++++ composer.json | 3 +- composer.lock | 55 +++++++++++++++++++++++++++++++++++- config/auth.php | 27 ++++++++++++++++++ 6 files changed, 171 insertions(+), 3 deletions(-) create mode 100644 app/middleware/Auth.php create mode 100644 app/middleware/UserLogin.php create mode 100644 config/auth.php diff --git a/app/BaseController.php b/app/BaseController.php index b3bd7d37..04c4fdc7 100644 --- a/app/BaseController.php +++ b/app/BaseController.php @@ -6,6 +6,8 @@ namespace app; use think\App; use think\exception\ValidateException; use think\Validate; +use app\middleware\UserLogin; +use app\middleware\Auth; /** * 控制器基础类 @@ -34,7 +36,10 @@ abstract class BaseController * 控制器中间件 * @var array */ - protected $middleware = []; + protected $middleware = [ + UserLogin::class, + Auth::class, + ]; /** * 构造方法 diff --git a/app/middleware/Auth.php b/app/middleware/Auth.php new file mode 100644 index 00000000..4941c883 --- /dev/null +++ b/app/middleware/Auth.php @@ -0,0 +1,38 @@ +controller()}/{$request->action()}"); + + // 初始化 user_id + $user_id = is_login(); + + // 权限检查 + $check = AuthHandle::check($rule, $user_id); + if (false === $check) { + $this->error('[403] 未授权访问'); + } + + return $next($request); + } +} diff --git a/app/middleware/UserLogin.php b/app/middleware/UserLogin.php new file mode 100644 index 00000000..fabf78bc --- /dev/null +++ b/app/middleware/UserLogin.php @@ -0,0 +1,44 @@ +top.location.href='/login';"; + } + + // 检验用户名或密码是否正确 + $yz = new \app\login\controller\Index; + $yz = $yz->check($username,$password); + if($yz['status'] == 0){ + echo ""; + } + + + return $next($request); + } +} diff --git a/composer.json b/composer.json index da028145..ffb29e9f 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,8 @@ "topthink/think-view": "^1.0", "topthink/think-template": "^2.0", "topthink/think-captcha": "^3.0", - "whitehat101/apr1-md5": "^1.0" + "whitehat101/apr1-md5": "^1.0", + "zzstudio/think-auth": "^2.0" }, "require-dev": { "symfony/var-dumper": "^4.2", diff --git a/composer.lock b/composer.lock index 2279e15c..8f4b3a61 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "5867a37e2dfac8abed05338ba1d7fe2b", + "content-hash": "53b1c190ba753cbb19c2f75a63b4695e", "packages": [ { "name": "league/flysystem", @@ -834,6 +834,59 @@ "apr1" ], "time": "2015-02-11T11:06:42+00:00" + }, + { + "name": "zzstudio/think-auth", + "version": "v2.0.2", + "source": { + "type": "git", + "url": "https://github.com/zz-studio/think-auth.git", + "reference": "6a060e9a668affe5dd50e72f8e4a3606df518aed" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/zz-studio/think-auth/zipball/6a060e9a668affe5dd50e72f8e4a3606df518aed", + "reference": "6a060e9a668affe5dd50e72f8e4a3606df518aed", + "shasum": "", + "mirrors": [ + { + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", + "preferred": true + } + ] + }, + "require": { + "php": ">=7.1.0", + "topthink/framework": "^6.0" + }, + "type": "library", + "extra": { + "think": { + "config": { + "auth": "src/config.php" + } + } + }, + "autoload": { + "psr-4": { + "think\\": "src/" + }, + "files": [ + "src/helper.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "authors": [ + { + "name": "byron", + "email": "xiaobo.sun@qq.com" + } + ], + "description": "The ThinkPHP6+ Auth Package", + "time": "2019-11-22T02:48:30+00:00" } ], "packages-dev": [ diff --git a/config/auth.php b/config/auth.php new file mode 100644 index 00000000..5b94bf99 --- /dev/null +++ b/config/auth.php @@ -0,0 +1,27 @@ + 1, // 权限开关 + 'auth_type' => 1, // 认证方式,1为实时认证;2为登录认证。 + 'auth_user' => 'admin', // 用户信息不带前缀表名 +]; \ No newline at end of file