【问题标题】:Using Different Session Namespaces with Zend Framework 2 Authentication Component在 Zend Framework 2 身份验证组件中使用不同的会话命名空间
【发布时间】:2012-08-02 15:15:35
【问题描述】:

我打算在未来的项目中使用 ZF2,所以我现在正在尝试 Zend Framework 2 RC1。我从身份验证步骤开始,并注意到当我为会话存储命名空间选择了与“Zend_Auth”不同的名称时,我无法访问存储在会话中的对象(AuthenticationService 类的 hasIdentity 方法返回 false,尽管会话中设置了用户对象数据)。

<?php
namespace Application\Controller;

use Zend\Authentication\Adapter\DbTable as AuthAdapter;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Storage\Session as SessionStorage;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Model\User;
use Application\Form\LoginForm;

class LoginController extends AbstractActionController
{
    public function indexAction()
    {
        $auth = new AuthenticationService();

        if ($auth->hasIdentity()) {
            return $this->redirect()->toRoute('application');
        }

        $form = new LoginForm();
        return array('form' => $form);
    }

    public function loginAction()
    {
        $auth = new AuthenticationService();

        $form = new LoginForm();
        $form->get('submit')->setAttribute('value', 'Add');

        $request = $this->getRequest();

        if ($request->isPost()) {
            $user = new User();
            $form->setInputFilter($user->getInputFilter('login'));

            $form->setData($request->getPost());
            if ($form->isValid()) {
                $data = $form->getData();

                // Configure the instance with constructor parameters...
                $sm          = $this->getServiceLocator();
                $dbAdapter   = $sm->get('db-adapter');
                $authAdapter = new AuthAdapter($dbAdapter, 'users', 'username', 'password');

                $authAdapter
                    ->setIdentity($data['username'])
                    ->setCredential(sha1($data['password']));

                // Use 'users' instead of 'Zend_Auth'
                $auth->setStorage(new SessionStorage('users'));

                $result = $auth->authenticate($authAdapter);

                if ($result->isValid()) {
                    // store the identity as an object where only the username and
                    // real_name have been returned
                    $storage = $auth->getStorage();

                    // store the identity as an object where the password column has
                    // been omitted
                    $storage->write($authAdapter->getResultRowObject(
                        null,
                        'password'
                    ));

                    // Redirect to list of application
                    return $this->redirect()->toRoute('application');
                }
            }    
        }

        // processed if form is not valid
        return array('form' => $form);
    }
}

在这段代码中,当我更改以下行时,

$auth->setStorage(new SessionStorage('users'));

像这样:

$auth->setStorage(new SessionStorage());

hasIdentity 方法返回 true。

我检查了 Zend\Authentication\AuthenticationService 和 Zend\Authentication\Storage\Session 两个类,但没有看到访问会话数据的方法,这些会话数据具有不同于默认的会话命名空间。

我需要了解的是如何访问具有不同命名空间的会话数据,如果目前无法访问,我们是否应该将其定义为错误?

如果需要任何其他信息,我可以更新问题。

【问题讨论】:

    标签: session authentication zend-framework2


    【解决方案1】:

    我们有点遗漏了您的代码的一部分,即您尝试接收用户身份的部分。我猜你忘记传递具有相同命名空间的 SessionStorage 对象。

    还应将身份验证对象的配置移至工厂,以免出现此类问题。

    那至少是我的 5 美分 :)

    【讨论】:

    • Antoine Hedgecock,抱歉我的回复晚了。我尝试接收用户身份的代码是上述代码的 indexAction。是的,当我尝试使用不同的命名空间时,我没有传递 SessionStorage 对象。现在我在想我们必须传递 SessionStorage 对象才能使用与默认 Zend_Auth 命名空间不同的命名空间(正如你在回答中提到的那样),这是真的吗?有没有其他简单的方法可以做到这一点,也许将命名空间字符串传递给 AuthenticationService 类?
    猜你喜欢
    • 2020-04-07
    • 2014-01-04
    • 2013-04-07
    • 2021-03-22
    • 1970-01-01
    • 1970-01-01
    • 2013-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多