合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
## 安装auth拓展 ``` composer require wenhainan/thinkphp6-auth ``` ## Auth配置文件 ![](https://img.kancloud.cn/87/6c/876cdb080b2d898648d8c7bf0ade99ed_933x294.png) ## 创建权限检查中间件 ``` php think make:middleware CheckAuth ``` ## 配置中间件别名 ![](https://img.kancloud.cn/81/4c/814c7bdf4dd79bceb7814353ce088054_1290x430.png) ## 在需要验证权限的控制器类中调用该中间件 ``` protected $middleware = ['auth']; ``` ## 权限处理中间件 ``` <?php declare (strict_types = 1); namespace app\middleware; use think\facade\Session; use think\wenhainan\Auth; //引入Auth类 use app\BaseController; use liliuwei\think\Jump; /** * 权限处理中间件 */ class CheckAuth extends BaseController { use Jump; /** * 处理请求 */ public function handle($request, \Closure $next) { // 调用权限检查方法 $this->checkAuth(); return $next($request); } /** * 权限检查 * @return bool */ function checkAuth() { //已登录用户id $id=session('id'); if (!$id) { $this->redirect('/admin/login'); } $module = request()->root(); //应用名 $controller = request()->controller(); //控制器名 $action = request()->action(); //方法名 // 请求到的规则名 AuthRule Name $url=$module . '/' . $controller . '/' . $action; //实例化Auth $auth = new Auth(); if (!$auth->check( $url , $id ) ) { // 没有权限跳转到未授权页面 $this->success("当前用户没有该操作权限,请联系管理员!",'../'); } } } ```