簡體   English   中英

如何手動(無形)驗證用戶? 蛋糕php 4x

[英]How to manually (formless) Authenticate user? 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;
    }

您正在設置身份驗證的主體信息,但您在下一個請求中丟失了它,因為它沒有持久化(我不讓您聽到“http 是無狀態的”歌曲......)

您的setIdentity也應該保持身份。 這可以通過不同的方式實現:

  • 在會話中,使用會話時
  • 在 JWT 令牌中,使用令牌時

以下是AuthenticationServicepersistIdentity中的persistIdentity 我建議你也看看JWT auth 配置

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM