Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
502 views
in Technique[技术] by (71.8m points)

php - ZendSessionContainer Session validation failed exception -- Object(Closure) ZF2

I'm trying to use authentication and sessions in a ZF2 App. So far I have the follow code:

In my Module.php:

// (...) rest of code

public function getServiceConfig()
    {
        return array(
                'factories' => array(
                        // (...) Other factories

                        // Authentication Service
                        'AuthService' => function($sm) {
                            $dbAdapter           = $sm->get('ZendDbAdapterAdapter');
                            $dbTableAuthAdapter  = new DbTable($dbAdapter,
                                'sec_user','login','password');

                            $authService = new AuthenticationService();
                            $authService->setAdapter($dbTableAuthAdapter);

                            return $authService;
                        },
                ),
        );
    }

// (...) rest of code

Then in my Controller Login action I have:

use ZendSessionContainer;

// (...) rest of code

    public function loginAction()
    {
       $this->getAuthService()->getAdapter()
                  ->setIdentity('testlogin')
                  ->setCredential('testpass');

        $auth_result = $this->getAuthService()->getAdapter()->authenticate();

        if ($auth_result->isValid()) {
            $session = new Container(); // Exception line
            $session->login = 'testlogin';

            // (...) other code
        }
    }
// (...) rest of code

The code works fine, validate the credentials successfully but when I try to save some value to SESSION with the simplest example I found in documentation, it fails. The line:

$session = new Container();

Is triggering this Exception:

Session validation failed

What else can I test? Any idea about what I'm doing wrong will be appreciated.

Bellow is the full trace:

File:
/var/www/sismedical/Server/vendor/zendframework/zendframework/library/Zend/Session/SessionManager.php:111


Message:

Session validation failed


Stack trace:

#0 /var/www/sismedical/Server/vendor/zendframework/zendframework/library/Zend/Session/AbstractContainer.php(78): ZendSessionSessionManager->start()
#1 /var/www/sismedical/Server/module/Security/src/Security/Controller/AuthController.php(100): ZendSessionAbstractContainer->__construct()
#2 /var/www/sismedical/Server/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractActionController.php(83): SecurityControllerAuthController->loginAction()
#3 [internal function]: ZendMvcControllerAbstractActionController->onDispatch(Object(ZendMvcMvcEvent))
#4 /var/www/sismedical/Server/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(ZendMvcMvcEvent))
#5 /var/www/sismedical/Server/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(207): ZendEventManagerEventManager->triggerListeners('dispatch', Object(ZendMvcMvcEvent), Object(Closure))
#6 /var/www/sismedical/Server/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractController.php(117): ZendEventManagerEventManager->trigger('dispatch', Object(ZendMvcMvcEvent), Object(Closure))
#7 /var/www/sismedical/Server/vendor/zendframework/zendframework/library/Zend/Mvc/DispatchListener.php(114): ZendMvcControllerAbstractController->dispatch(Object(ZendHttpPhpEnvironmentRequest), Object(ZendHttpPhpEnvironmentResponse))
#8 [internal function]: ZendMvcDispatchListener->onDispatch(Object(ZendMvcMvcEvent))
#9 /var/www/sismedical/Server/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(ZendMvcMvcEvent))
#10 /var/www/sismedical/Server/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(207): ZendEventManagerEventManager->triggerListeners('dispatch', Object(ZendMvcMvcEvent), Object(Closure))
#11 /var/www/sismedical/Server/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php(309): ZendEventManagerEventManager->trigger('dispatch', Object(ZendMvcMvcEvent), Object(Closure))
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Attaching an event listener to listen to all events triggered by all classes $sharedEvents->attach('*', '*', .. ) will cause this error message.

You have to be really careful what you attach listeners to, since some listeners are expecting a specific response (in this case a boolean indicating if session is valid). A listener that wasn't designed to handled the event, such as a log listener, will typically not return a response, which in this case is seen as a validation failure.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...