0

这个问题在堆栈溢出中被问了很多次,但我尝试了每个接受的解决方案

我是蛋糕 PHP 的新手,我被分配在我们的应用程序中添加 JWT。以前,团队使用默认的蛋糕会话。为了集成,我使用了admad/cakephp-jwt-auth. 所以在 AppController

public function initialize()
    {
        parent::initialize();
        $this->loadComponent('RequestHandler');
        $this->loadComponent('Flash');
        $this->loadComponent('Recurring');
        $this->loadComponent('Auth', [
                'storage' => 'Memory',
                'authenticate' => [
                    'Form' => [
                        'fields' => [
                            'username' => 'user_name',
                            'password' => 'password',
                        ],
                        'contain' => ['Roles']
                    ],
                    'ADmad/JwtAuth.Jwt' => [
                        'parameter' => 'token',
                        'userModel' => 'CbEmployees',
                        'fields' => [
                            'username' => 'id'
                        ],
                        'queryDatasource' => true
                    ]
                ],
                'unauthorizedRedirect' => false,
                'checkAuthIn' => 'Controller.initialize'
            ]);
}

我必须使用CbEmployees哪个是我们的用户模型。

然后在我的自定义控制器中,添加我的登录功能

public function login()
    {
        $user = $this->Auth->identify();
        if (!$user) {
            $data = "Invalid login details";
        } else {
            $tokenId  = base64_encode(32);
            $issuedAt = time();
            $key = Security::salt();
            $data = JWT::encode(
                [
                    'alg' => 'HS256',
                    'id' => $user['id'],
                    'sub' => $user['id'],
                    'iat' => time(),
                    'exp' =>  time() + 86400,
                ],
                $key
            );
        }
        $this->ApiResponse([
            "data" => $data
        ]);
    }

然后我使用 postman 和 body 调用这个函数

{
    "username": "developer",
    "password": "dev2020"
}

我总是得到回应Invalid login details。所以建议的解决方案是检查密码数据类型和长度。密码是 varchar(255)。另一种解决方案是检查实体中的密码。在我拥有的实体中

protected function _setPassword($password)
    {
        if (strlen($password) > 0) {
            return Security::hash($password, 'sha1', true);
            // return (new DefaultPasswordHasher)->hash($password);
        }
    }

我特别问为什么团队使用Security::hash($password, 'sha1', true);蛋糕 2 到蛋糕 3 他们必须使用相同的迁移。

为什么我总是得到Invalid login details?我在这里做错了什么?使用该应用程序时,我可以使用相同的凭据登录。

4

0 回答 0