php - Zend2 + Doctrine2 认证

标签 php authentication doctrine-orm zend-framework2

我想使用 Doctrine 2 和 ZF2 进行身份验证。为了获得帮助,我使用了 Zend 2 + doctrine 2 Auth Adapter ,但每次我调用 $authService->authenticate($adapter); 时,我都会收到一个错误,指出类“”不存在。

我的 module.config.php 中的配置似乎不会被使用。显示如下:

'authenticationadapter' => array(
        'orm_default' => array(
            'objectManager' => 'doctrine.entitymanager.orm_default',
            'identityClass' => 'Profile\Entity\User',
            'identityProperty' => 'username',
            'credentialProperty' => 'password',
        ),
    ),

但是如果我在 authService 上创建了一个 var_dump,所有的集合都是空的。 在我想要登录的服务中,我调用

$authAdapter = $this->getAuthAdapter();
$authAdapter->setIdentityValue($username);
$authAdapter->setCredentialValue($password);

$authAdapter 的转储告诉我 IdentityValue 和 CredentialValue 设置正确。

$authAdabter 中的其他内容是:

protected 'options' => 
    object(DoctrineModule\Options\Authentication)[281]
      protected 'objectManager' => 
        object(Doctrine\ORM\EntityManager)[248]
          private 'config' => 
            object(Doctrine\ORM\Configuration)[250]
              ...
          private 'conn' => 
            object(Doctrine\DBAL\Connection)[252]
              ...
          private 'metadataFactory' => 
            object(Doctrine\ORM\Mapping\ClassMetadataFactory)[266]
              ...
          private 'repositories' => 
            array (size=0)
              ...
          private 'unitOfWork' => 
            object(Doctrine\ORM\UnitOfWork)[267]
              ...
          private 'eventManager' => 
            object(Doctrine\Common\EventManager)[242]
              ...
          private 'hydrators' => 
            array (size=0)
              ...
          private 'proxyFactory' => 
            object(Doctrine\ORM\Proxy\ProxyFactory)[268]
              ...
          private 'expressionBuilder' => null
          private 'closed' => boolean false
          private 'filterCollection' => null
      protected 'objectRepository' => null
      protected 'identityClass' => null
      protected 'identityProperty' => null
      protected 'credentialProperty' => null
      protected 'credentialCallable' => null
      protected 'classMetadata' => null
      protected 'storage' => null
      protected '__strictMode__' => boolean true
  protected 'authenticationResultInfo' => null

getAuthAdapter 显示如下:

public function getAuthAdapter()
{
    if (null === $this->authAdapter) {
        $this->authAdapter = $this->getServiceManager()
            ->get('doctrine.authenticationadapter.ormdefault');
    }
    return $this->authAdapter;
}

那么有人能告诉我如何正确设置选项吗?

最佳答案

您似乎使用了错误的配置值。如果您查看 DoctrineORM 文档,他们使用以下内容来设置身份验证适配器的配置:

'doctrine' => array(
    'authentication' => array(
        'orm_default' => array(
            'object_manager' => 'Doctrine\ORM\EntityManager',
            'identity_class' => 'Application\Entity\User',
            'identity_property' => 'email',
            'credential_property' => 'password',
        ),
    ),
)

因此,不要使用 authenticationadapter,而是在您的 module.config.php 中使用 authentication;而不是使用 objectManager 使用 object_manager 等。


在您的 Module.php 中,您需要添加以下内容:

use Zend\Authentication\AuthenticationService;

...

public function getServiceConfig()
{
    return array(
        'factories' => array(
            'Zend\Authentication\AuthenticationService' => function($serviceManager) {
                return $serviceManager->get('doctrine.authenticationservice.orm_default');

            }
        )
    );
}

在您的 Controller 中,您可以使用以下方式访问适配器:

$authService = $this->getServiceLocator()->get('Zend\Authentication\AuthenticationService');

$adapter = $authService->getAdapter();
$adapter->setIdentityValue($data['login']);
$adapter->setCredentialValue($data['password']);

请关注documentation .

关于php - Zend2 + Doctrine2 认证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15175504/

相关文章:

php - Eloquent order by one-to-one-related column

javascript - 条形图、chart.js PHP 将无法加载

java - API访问认证java

php - 无法使用 Symfony 和 Doctrine 连接到 SQLite DB

doctrine - 在带有 DQL 的 WHERE 子句中使用 DATE() 函数

php - 如果使用 PHP 用户的时区发生更改,如何处理存储在数据库中的用户数据

php - PHP 中的 SSL 帖子

apache - 通过 AJP 将 REMOTE_USER 转发到 tomcat(例如,用于 shibboleth)

jsf - 使特定用户的 session 无效

php - 原则对产品有问题,但对开发没有问题