让我们从出了什么问题开始:我有一个 acl 侦听器连接到"route"事件,起初看起来工作正常,但一段时间后会抛出几个错误。这些是错误:
致命错误:未捕获的异常 "Zend''Session''Exception''RuntimeException",消息为"Session 验证失败'...
Zend''ServiceManager''Exception''ServiceNotCreatedException:一个异常 在创建"身份验证服务"时提出;未返回实例...
Zend''ServiceManager''Exception''ServiceNotCreatedException:一个异常 在创建"MyAclListener"时被提出;未返回实例...
侦听器的设置如下:
class MyAclListener implements ListenerAggregateInterface
{
/** @var AuthenticationServiceInterface */
protected $_authService;
function __construct (
AuthenticationServiceInterface $authService ,
)
{
$this->_authService = $authService;
}
public function attach ( EventManagerInterface $events )
{
$this->listeners[ ] = $events->attach( 'route' , array( $this , 'checkAcl' ) );
}
public function checkAcl( MvcEvent $e ) {
'''Do Something for which the authService is needed
}
然后,在模块.config中.php我有
$config = array(
'service_manager' => array(
'factories' => array(
'MyAclListener' => 'MyAcl'Factory'MyAclListenerFactory'
) ,
) ,
'listeners' => array(
'MyAclListener'
) ,
);
侦听器的工厂:
<?php
namespace MyAcl'Factory;
use MyAcl'Listener'MyAclListener;
use Zend'ServiceManager'FactoryInterface;
use Zend'ServiceManager'ServiceLocatorInterface;
class AclListenerFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
// Finally create the service
return new MyAclListener(
$serviceLocator->get('authService') ,
);
}
}
会话在模块中设置.php
public function onBootstrap ( MvcEvent $e )
{
$serviceManager = $e->getApplication()->getServiceManager();
$config = $serviceManager->get('Configuration');
$this->initSession( $config['session'] , $serviceManager->get('sessionCache') );
}
public function initSession ( $config , $cache )
{
$sessionConfig = new SessionConfig();
$sessionConfig->setOptions( $config );
$sessionManager = new SessionManager( $sessionConfig );
$saveHandler = new Cache($cache);
$sessionManager->setSaveHandler( $saveHandler );
$sessionManager->getValidatorChain()->attach('session.validate', array(new HttpUserAgent(), 'isValid'));
$sessionManager->getValidatorChain()->attach('session.validate', array(new RemoteAddr(), 'isValid'));
$sessionManager->start();
$sessionManager->regenerateId(true);
}
这些是会话的相关配置:
$config = array(
'session' => array(
'cookie_lifetime' => 28800 ,
'remember_me_seconds' => 28800 ,
'use_cookies' => true ,
'cookie_httponly' => true ,
) ,
'caches' => array(
'sessionCache' => array(
'adapter' => array(
'name' => 'memcached' ,
'lifetime' => 604800 ,
'options' => array(
'servers' => array(
array(
'127.0.0.1' , 11211
)
) ,
'namespace' => 'MySessionCache' ,
'liboptions' => array(
'COMPRESSION' => true ,
'binary_protocol' => true ,
'no_block' => true ,
'connect_timeout' => 100
)
) ,
'plugins' => array( 'serializer' )
) ,
) ,
) ,
)
知道在错误发生后我的"身份"丢失并且登录用户被注销也可能很有用。
那么,我做错了什么?如何摆脱这些错误?甚至可能:是否有更好的解决方案来设置 ACL 检查和我的会话?
更新
我更改了会话设置:
在模块中.php
public function onBootstrap ( MvcEvent $e )
{
$serviceManager = $e->getApplication()->getServiceManager();
/* session */
$this->expandInitialSessionConfig( $serviceManager , $serviceManager->get( 'sessionCache' ) );
}
public function expandInitialSessionConfig ( $sm , $cache )
{
/** @var 'Zend'Session'SessionManager $sessionManager */
$sessionManager = $sm->get( 'Zend'Session'SessionManager' );
$saveHandler = new Cache( $cache );
$sessionManager->setSaveHandler( $saveHandler );
$sessionManager->getValidatorChain()->attach( 'session.validate' , array( new HttpUserAgent() , 'isValid' ) );
$sessionManager->getValidatorChain()->attach( 'session.validate' , array( new RemoteAddr() , 'isValid' ) );
}
在模块配置中.php
return array(
'session_config' => array(
'cookie_lifetime' => 28800 ,
'remember_me_seconds' => 28800 ,
'use_cookies' => true ,
'cookie_httponly' => true ,
'gc_probability' => 10 ,
'gc_maxlifetime' => 7200 ,
'cookie_domain' => 'mydomain.com' ,
) ,
'service_manager' => array(
'factories' => array(
'Zend'Session'SessionManager' => 'Zend'Session'Service'SessionManagerFactory' ,
'Zend'Session'Config'ConfigInterface' => 'Zend'Session'Service'SessionConfigFactory' ,
) ,
);
并且$sessionManager->regenerateId( true ) 被移动,所以它现在只在登录时发生。
您向AclListenerFactory
中的ServiceLocator
询问authService
,但它找不到导致第二个异常的任何名称。由于找不到authService
,因此工厂不会返回侦听器。这是最后一个例外。
在代码中,您编写:
$serviceLocator->get('authService') ,
第一个异常与某些会话验证失败有关。所有问题的原因可能就在那里。没有会话验证可能会导致没有有效的authService
从而链接其他两个异常。此authService
在哪里注册或创建?这在您当前共享的代码中不可见。以及此服务如何依赖于失败的"验证"......
如果您解决了会话验证问题,则一切可能都很好。
我严重怀疑这是"最佳实践",但是从我的听众的构造函数中删除那些取决于会话的缺点(自原始问题以来添加了更多)解决了这个问题。我知道在代码中需要它们的第一刻从服务定位器获取它们,并将它们存储在侦听器类的受保护参数中以供进一步使用。但是,就像我说的,这不再是正确的依赖注入,但至少它可以工作。
function __construct (Logger $_myLog )
{
$this->_myLog = $_myLog;
// removed here: $this->_authService = $authService;
}
然后:
public function checkAcl ( MvcEvent $e )
{
$sm = $e->getApplication()->getServiceManager();
$this->_authService = $sm->get('authService');
....
}