使用自定义身份验证适配器时,检查CakePHP上的登录用户信息

时间:2016-11-10 18:32:34

标签: php authentication cakephp jwt

我正在使用this JWTAuth adapter在我的CakePHP 2.8应用中使用JWT身份验证而不是基于cookie的身份验证。它很棒,除了一个故障:

通常,对于我的一个REST端点,我可以使用$this->Auth->user("id")来获取当前登录用户的ID。

当我尝试使用$this->Auth->allow()使非成员可以访问控制器操作时,会出现问题。如果我这样做,在控制器中使用$this->Auth->loggedIn()会返回false,这意味着我无法为登录用户添加其他逻辑。

使用标准Cookie身份验证时:

  • $this->Auth->user('id')可用于 Controller::beforeFilter()
  • $this->Auth->loggedIn()trueController::beforeFilter()
  • $this->Auth->user('id')在控制器操作中可用,公开 和成员。
  • $this->Auth->loggedIn()在控制器操作中是true,是公开的 和成员。

使用JWT身份验证时:

  • $this->Auth->user('id')null中为Controller::beforeFilter()
  • $this->Auth->loggedIn()falseController::beforeFilter()
  • $this->Auth->user('id')仅适用于成员控制器操作,null适用于公共控制器操作。
  • $this->Auth->loggedIn()在仅限成员的控制器操作中为true,在公共控制器操作中为false

有什么方法可以让Auth将JWTAuth组件返回的信息包含在$this->Auth->allow()公开的操作中?

此处的控制器示例:

public function visible(){
    // This will always be false, even if a valid JWT token is sent
    $this->set("loggedIn", $this->Auth->loggedIn());
}

public function members_only(){
    // This will be unavailable if not logged in, and a true if logged in
    $this->set("loggedIn", $this->Auth->loggedIn());
}

public function beforeFilter($options = array()){
    parent::beforeFilter();

    $this->Auth->allow("visible");
}

供参考,我的AppController :: components数组;

public $components = array(
    'DebugKit.Toolbar',
    'Auth' => array(
        'authorize' => array(
            'Actions' => array(
                'actionPath' => 'controllers'
            ),
        ),
        'authenticate' => array(
            'Form' => array(
                'fields' => array('username' => 'email'),
                'contain' => array(
                    'UserProfile',
                )
            ),
            'JwtAuth.JwtToken' => array(
                'fields' => array(
                    'username' => 'email',
                    'token' => 'password',
                ),
                'header' => 'AuthToken',
                'userModel' => 'User',
            ),
        ),
        'unauthorizedRedirect' => false
    ),
    "Acl",
    "RequestHandler",
    "Session"
);

2 个答案:

答案 0 :(得分:2)

对于无状态适配器,在AuthComponent::startup()中触发了身份验证过程。组件的startup()方法在 Controller::beforeFilter()后运行,这就是AuthComponent::user()不返回任何信息的原因。

对于其他适配器,当用户通过身份验证时,身份信息将存储在会话中。获取该信息不需要任何身份验证过程,这就是AuthComponent::user()在标准基于cookie的身份验证的情况下为您提供用户信息的原因。

答案 1 :(得分:0)

我来晚了,但是我找到了解决这个问题的方法,但是(警告!)它涉及到更新AuthComponent的代码。

我复制了Lib / Controller / Component / AuthComponent.php并将其放在app / Controller / Component / AuthComponent.php下。

然后我在文件中添加了一行:

public function startup(Controller $controller) {
    $methods = array_flip(array_map('strtolower', $controller->methods));
    $action = strtolower($controller->request->params['action']);

    // One-line modification from the ordinary CakePHP file.
    // This lets us have info on the logged-in user in public actions when using stateless auth.
    $this->_getUser();

Voila,您现在可以在使用无状态身份验证访问不受保护的控制器功能的同时访问服务器上的用户信息!