问题描述
$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令牌
这是AuthenticationService
在persistIdentity
中的工作方式。我建议您也看看JWT auth configuration。