我已经创建了一个基于cookie的“记住我”的登录系统,我使用的是ZF2和ZfcUser和BjyAuthorize。那么,在登录后,如果缓存过期,BjyAuthorize触发器将出现在我的“记住我”系统之前,用户将无法识别为已登录。
这就是我在我的AbstractController:https://gist.github.com/Gamempire/2b6b6388cedca9491d5f#file-abstractcontroller-php-L18 (每个控制器都扩展了我的AbstractController)中称为“记住我”的cookie系统的地方。
我如何在BjyAuthorize之前调用它,这样BjyAuthorize就知道有人登录了?
谢谢
发布于 2014-11-22 20:20:42
要正确地做到这一点,我不只是尝试在bjyAuthorize之前注入自己,最好添加到ZfcUser功能中,并使用storageChain来获取用户的身份。
若要通过重写ZfcUser的服务名称来执行此操作,请执行以下操作
'service_manager' => array(
'factories' => array(
'zfcuser_auth_service' => 'myNamespace\Factory\AuthenticationServiceFactory',
),
),之后,您需要创建您自己的工厂,您可以复制ZfcUser并对其进行更改。
public function createService(ServiceLocatorInterface $serviceLocator)
{
$storageChain = new \Zend\Authentication\Storage\Chain();
// dummy storage interface
$nonPersistent = new \Zend\Authentication\Storage\NonPersistent(); // replace this with your own storage interface.
$nonPersistent->write(1); // just to test
/* @var $authStorage Storage\StorageInterface */
$authStorage = $serviceLocator->get('ZfcUser\Authentication\Storage\Db');
$storageChain->add($authStorage, 10); // Check this interface first.
$storageChain->add($nonPersistent, 0); // Check this interface Second.
/* @var $authAdapter Adapter\AdapterInterface */
$authAdapter = $serviceLocator->get('ZfcUser\Authentication\Adapter\AdapterChain');
return new AuthenticationService(
$storageChain,
$authAdapter
);
}使用此示例代码,当没有登录用户的会话时,$this->zfcUserAuthentication()->getIdentity()将返回1。
在此之后,您需要创建自己的存储接口。它实现Zend\Authentication\Storage\StorageInterface。我不知道你的cookie实现是怎样的,你需要让它适应这一点。此外,您还需要实现何时使用cookie和何时不使用cookie。还有一个为zfcuser创建的模块,它实现了一个名为GoalioRememberMe的me函数。希望这能帮到你。
https://stackoverflow.com/questions/26962805
复制相似问题