如何手动无格式验证用户身份? Cakephp 4x

问题描述

当我使用setIdentity时,用户不会保持登录状态。

    $user = $this->Users->get(1);
    $this->Authentication->setIdentity($user);
    $userInfo = $this->Authentication->getIdentity(); // Returns good.
    $this->redirect('Somewhere');

某处

    $userInfo = $this->Authentication->getIdentity(); // Returns null.

我没有使用表格。仅基于某些算法动态设置用户就不会发生POST ...

Application.PHP

public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface
    {
        $service = new AuthenticationService([
        'unauthenticatedRedirect' => '/login','queryParam' => 'redirect',]);

/*        $fields = [
            'username' => 'username','password' => 'password'
        ];

        $service->loadIdentifier('Authentication.Password',compact('fields')); */

        // Load the authenticators,you want session first
        $service->loadAuthenticator('Authentication.Session');

        return $service;
    }

解决方法

您正在设置Authentication上的主体信息,但是由于它不存在,因此在下一个请求上将其释放(我为您省却了“ http是无状态”歌曲...)

您的setIdentity的一部分也应该保留身份。这可以通过不同的方式实现:

  • 在会话中,使用会话时
  • 使用令牌时,使用JWT令牌

这是AuthenticationServicepersistIdentity中的工作方式。我建议您也看看JWT auth configuration

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...