中间件
This commit is contained in:
1 parent
90a008612e
commit
5182111a65
6 files changed
+171
-3
No files matched your search
@@ -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,
|
||||
];
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\middleware;
|
||||
|
||||
use think\exception\HttpResponseException;
|
||||
use think\Auth AS AuthHandle;
|
||||
use traits\controller\Jump;
|
||||
|
||||
class Auth
|
||||
{
|
||||
use Jump;
|
||||
/**
|
||||
* 处理请求
|
||||
*
|
||||
* @param \think\Request $request
|
||||
* @param \Closure $next
|
||||
* @return Response
|
||||
*/
|
||||
public function handle($request, \Closure $next)
|
||||
{
|
||||
// 白名单
|
||||
$allow = ['user/login'];
|
||||
|
||||
$rule = strtolower("{$request->controller()}/{$request->action()}");
|
||||
|
||||
// 初始化 user_id
|
||||
$user_id = is_login();
|
||||
|
||||
// 权限检查
|
||||
$check = AuthHandle::check($rule, $user_id);
|
||||
if (false === $check) {
|
||||
$this->error('[403] 未授权访问');
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\middleware;
|
||||
|
||||
class UserLogin
|
||||
{
|
||||
/**
|
||||
* 处理请求
|
||||
*
|
||||
* @param \think\Request $request
|
||||
* @param \Closure $next
|
||||
* @return Response
|
||||
*/
|
||||
public function handle($request, \Closure $next)
|
||||
{
|
||||
|
||||
// 尝试从session中获取用户名和密码
|
||||
$username = session('?username') ? $username = session('username') : $username = '';
|
||||
$password = session('?password') ? $password = session('password') : $password = '';
|
||||
|
||||
// 尝试从cookie中获取用户名和密码
|
||||
if( strlen($username)<1 )
|
||||
{
|
||||
$username = cookie('?username') ? $username = cookie('username') : $username = '';
|
||||
$password = cookie('?password') ? $password = cookie('password') : $password = '';
|
||||
}
|
||||
// 如果没有获取到用户名和密码,则跳转到登录页面
|
||||
if( strlen($username)<1 )
|
||||
{
|
||||
echo "<script>top.location.href='/login';</script>";
|
||||
}
|
||||
|
||||
// 检验用户名或密码是否正确
|
||||
$yz = new \app\login\controller\Index;
|
||||
$yz = $yz->check($username,$password);
|
||||
if($yz['status'] == 0){
|
||||
echo "<script>top.location.href='login';</script>";
|
||||
}
|
||||
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
Loaded 3 of 6 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user