关于使用AuthenticationPlugin'实现记住我;s Cookie验证器

关于使用AuthenticationPlugin'实现记住我;s Cookie验证器,authentication,cakephp,cookies,cakephp-4.x,Authentication,Cakephp,Cookies,Cakephp 4.x,我使用CakePHP的AuthenticationPlugin。我试着在这个系统中实现Memberme功能。 我在阅读Cakephp文档时发现了以下文章。 然而,这里的文档对我来说很难理解。我不知道该怎么办。 我已经成功地实现了EncryptedCookie中间件。我不知道那之后该怎么办。 我不知道如何使用rememberMeField,如何使用字段,如何使用cookies $this->Authentication->rememberMeField $this->Authentication

我使用CakePHP的AuthenticationPlugin。我试着在这个系统中实现Memberme功能。 我在阅读Cakephp文档时发现了以下文章。

然而,这里的文档对我来说很难理解。我不知道该怎么办。 我已经成功地实现了EncryptedCookie中间件。我不知道那之后该怎么办。 我不知道如何使用rememberMeField,如何使用字段,如何使用cookies

$this->Authentication->rememberMeField

$this->Authentication->fields

我试着看看我是否能像这样使用它,但还是不行。 请让我知道如何使用这些。 还有,你知道有什么记忆教程吗? 我如何实现它

对不起。请帮帮我


//在config/app.php中
“安全性”=>[
.....
'cookieKey'=>env('SECURITY_COOKIE_KEY','AnyString'),//getData('rememberMe');获取它。
如果该值为1,则复选框已按下

//在src/Controller/userscocontroller中
公共函数登录()
{
$this->request->allowMethod(['get','post']);
如果($this->request->is('post')){
$result=$this->Authentication->getResult();
//如果用户登录,无论是POST还是GET,我们都将重定向
$requestGetData=$this->request->getData('rememberMe');
if($requestGetData['rememberMe']==1){
$this->Authentication->cookie['name']=$requestGetData['mail'];
$this->Authentication->cookie['name']=$requestGetData['password']
}
如果($result->isValid()){
$redirect=$this->request->getQuery('redirect')[
'控制器'=>'存储',
“操作”=>“索引”,
]);
返回$this->redirect($redirect);
}
//如果用户在提交后未能进行身份验证,则会显示错误。
如果(!$result->isValid()){
$this->Flash->error(_('您的电子邮件地址或密码不正确');
}
}
$title=$this->config('Users.title.login');
$message=$this->config('Users.message.login');
$this->set(压缩(“立即登录”、“标题”、“消息”);
}
我知道那不是真的。但我试着实现类似的东西以防万一。 请帮帮我


在登录前后更改

公共函数登录()
{
$this->request->allowMethod(['get','post']);
如果($this->request->is('post')){
$result=$this->Authentication->getResult();
$requestData=$this->request->getData();
如果($result->isValid()){
$redirect=$this->request->getQuery('redirect')[
'控制器'=>'存储',
“操作”=>“索引”,
]);
$this->Authentication->getAuthenticationService()->loadAuthenticator('Authentication.Cookie'[
'字段'=>['邮件','密码']
]
);
返回$this->redirect($redirect);
}
如果($this->request->is('post')&&!$result->isValid()){
$this->Flash->error(_('您的电子邮件地址或密码不正确');
}
}
$title=$this->config('Users.title.login');
$message=$this->config('Users.message.login');
$this->set(压缩('title','message');
}

您不应该在控制器中加载身份验证程序,身份验证在调用任何控制器之前在中间件级别进行

cookie验证器的加载和配置与任何其他验证器一样,也就是您创建身份验证服务的地方,通常在
src/Application.php中的
Application::getAuthenticationService()

默认情况下,表单中的字段必须是
rememberMe
,而不是
rememberMe
,除非您配置cookie验证器的
rememberMeField
选项

此外,cookie验证器的默认cookie名称是
CookieAuth
,因此如果要对其进行加密,则必须在
EncryptedCookieAuth
配置中相应地使用该名称

太长,读不下去了 从控制器中删除所有与cookie相关的代码,并在
应用程序::getAuthenticationService()
方法中加载验证器:

使用Authentication\Identifier\IdentifierInterface;
// ...
公共函数getAuthenticationService(ServerRequestInterface$request):AuthenticationServiceInterface
{
$service=newauthenticationservice();
// ...
//cookie验证器应在会话验证器之后加载,
//和u在u之前u像表单验证器这样的其他验证器
$service->loadAuthenticator('Authentication.Cookie'[
//'rememberMeField'=>'custom\u form\u field\u name',//如果要更改默认值
“字段”=>[
IdentifierInterface::凭证\用户名=>“邮件”,
IdentifierInterface::CREDENTIAL_PASSWORD=>“PASSWORD”,
],
]);
// ...
返回$service;
}
EncryptedCookieMiddleware
config:

$cookies=新加密Cookie中间件(
['CookieAuth'],
Configure::read('Security.cookieKey')
);
如果使用cookie验证器的默认值,请将表单中的字段名更改为
记住我

echo$this->Form->control('记住我')[
'键入'=>'复选框'
]);
这就是所需的全部内容,如果您在登录表单中勾选复选框,那么认证中间件将在成功认证acc后设置cookie