Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用自定义身份验证适配器时检查CakePHP上登录的用户信息_Php_Authentication_Cakephp_Jwt - Fatal编程技术网

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

使用自定义身份验证适配器时检查CakePHP上登录的用户信息,php,authentication,cakephp,jwt,Php,Authentication,Cakephp,Jwt,我在CakePHP2.8应用程序中使用JWT身份验证而不是基于cookie的身份验证。它工作得很好,除了一个故障: 通常,对于我的一个REST端点,我可以使用$this->Auth->userid来获取当前登录的用户ID 当我尝试使用$this->Auth->allow使非成员可以访问控制器操作时,出现了一个问题。如果我这样做,在控制器中使用$this->Auth->loggedIn返回false,这意味着我不能为登录用户添加额外的逻辑 使用标准cookie身份验证时: $this->Auth-

我在CakePHP2.8应用程序中使用JWT身份验证而不是基于cookie的身份验证。它工作得很好,除了一个故障:

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

当我尝试使用$this->Auth->allow使非成员可以访问控制器操作时,出现了一个问题。如果我这样做,在控制器中使用$this->Auth->loggedIn返回false,这意味着我不能为登录用户添加额外的逻辑

使用标准cookie身份验证时:

$this->Auth->user'id'在中可用 控制器::beforeFilter。 $this->Auth->loggedIn在中为真 控制器::beforeFilter。 $this->Auth->user'id'在控制器操作中可用,public 仅限成员。 $this->Auth->loggedIn在控制器操作中为true,public 仅限成员。 使用JWT auth时:

$this->Auth->user'id在Controller::beforeFilter中为空。 $this->Auth->loggedIn在中为false 控制器::beforeFilter。 $this->Auth->user'id在仅成员控制器操作中可用,在公共控制器操作中为null。 $this->Auth->loggedIn在仅成员控制器操作中为true,在公共控制器操作中为false。 有没有办法让Auth包含JWTAuth组件返回的关于$this->Auth->allow公开的操作的信息

此处的示例控制器:

作为参考,我的AppController::components数组


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


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

我有点晚了,但我找到了解决此问题的方法,但警告!它涉及更新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();

瞧,您现在可以访问服务器上的用户信息,同时使用无状态身份验证访问无保护的控制器函数

只是想提一下,在CakePHP 3.x中,AuthComponent可以配置为在控制器的beforeFilter之前运行的initialize方法中运行身份验证过程。关于'CakePHP 2',我如何在beforeFilter中获取用户信息?我认为这个解决方案只适用于Cakephp3@KenanaReda请看我的答案-我找到了解决方案!已经有了检查_getuser的条件,所以只添加这一行没有任何区别,它仍然没有在beforefilter中标识用户。是否有其他配置?嗨,Kenana,我不知道您使用的是什么无状态身份验证系统。您可以使用此方法修补AuthComponent,以查找可能需要进行的修改@caitlin请避免更新源代码,因为升级cake版本时会丢失更改。相反,请尝试我的解决方案。@Farhan当您像我一样进行更改时,当您升级库时,更改不会被覆盖。我看不出你的解决办法。您在这里发布了吗?@caitlin您可以在beforeFilter中将auth组件初始化为$this->auth->startup$this;
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"
);
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();