[英]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
也應該保持身份。 這可以通過不同的方式實現:
以下是AuthenticationService
在persistIdentity
中的persistIdentity
。 我建議你也看看JWT auth 配置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.