zoukankan      html  css  js  c++  java
  • yii Auth 自定义

    在yii 开发中一个项目内实现多个不同的user Auth 认证

     

    首先把不需要此认证的模块控制器过滤掉

    public function behaviors()
        {
            return [
                'verbs' => [
                    'class' => VerbFilter::className(),
                    'actions' => [
                        '*' => ['POST'],
                    ],
                ],
                'bearerAuth' => [
                    'class' => UserAuth::className(),
                    'except' => [
                        'carray/*',
                        'sys/login',
                    ],
                    'exceptModules' => true
                ]
            ];
        }

     

    新建一个认证 ; 重写authenticate;

    重新定义identityClass
    在需要的模块或者模型中重新写入 
    behaviors;
    /**
         * {@inheritDoc}
         */
        public function authenticate($user, $request, $response)
        {
            $authHeader = $request->getHeaders()->get($this->header);
    
            if ($authHeader !== null) {
                if ($this->pattern !== null) {
                    if (preg_match($this->pattern, $authHeader, $matches)) {
                        $authHeader = $matches[1];
                    } else {
                        return null;
                    }
                }
    
                Yii::$app->user->identityClass = MemberModel::className();
                $identity = $user->loginByAccessToken($authHeader, get_class($this));
    
                if ($identity === null) {
                    $this->challenge($response);
                    $this->handleFailure($response);
                }
    
                return $identity;
            }
    
            return null;
        }

     

  • 相关阅读:
    Go之运算符
    前端开发之工具库
    MVC与MVVM
    开发工具之Vscode编辑器
    常用名词汇总
    python常见错误总结
    Python之常用第三方库总结
    PHP程序员的成长路线
    web 应用常见安全漏洞
    redis和memcached的区别详解
  • 原文地址:https://www.cnblogs.com/sw-3/p/11199836.html
Copyright © 2011-2022 走看看