【问题标题】:Using mongodb for user authentication in symfony 2.1在 symfony 2.1 中使用 mongodb 进行用户认证
【发布时间】:2012-03-26 17:05:18
【问题描述】:

在 Symfony 2.1 的当前版本中,应该可以将 MongoDB 用作 SecurityBundle 的 Userprovider,而无需使用 FOSUserBundle(此处介绍:mongodb symfony user authentication?)。

想不通,代码的问题到底出在哪里,但我无法使用预定义用户test:test登录。

我的security.yml 看起来像这样:

安全: 编码器: test\TestBundle\Document\User:纯文本 提供者: 文档成员: mongodb:{类:testTestBundle:用户,属性:用户名} 防火墙: 安全区域: 模式:^/ http_basic: 领域:“登录测试” 访问控制: - { 路径:^/admin,角色:ROLE_ADMIN } 角色层次结构: ROLE_ADMIN:ROLE_USER

test/TestBundle/Document/User.php-文档:

命名空间测试\TestBundle\Document; 使用 Symfony\Component\Security\Core\User\UserInterface; 使用 Symfony\Component\Security\Core\User\EquatableInterface; 使用 Doctrine\ODM\MongoDB\Mapping\Annotations 作为 ODM; /** * @ODM\Document(collection="user", repositoryClass="test\TestBundle\Document\UserRepository") */ 类 User 实现 UserInterface、EquatableInterface { /** * @ODM\ID */ 受保护的$id; /** * @ODM\字符串 */ 受保护的$用户名; /** * @ODM\字符串 */ 受保护的$密码; /** * @ODM\集合 */ 受保护的 $roles = array(); /** * @ODM\字符串 */ 受保护的$盐; /** * @ODM\布尔值 */ 受保护的$isActive; // 设置器 /** * @param 字符串 */ 公共函数 setUsername($username) { $this->用户名 = $用户名; } /** * @param 字符串 */ 公共函数 setPassword($password) { $this->密码 = $密码; } /** * @param 字符串 */ 公共函数 setRole($role) { $this->角色[] = $角色; } /** * @param 数组 */ 公共函数 setRoles(数组 $roles) { $this->roles = (array) $roles; } /** * @param 字符串 */ 公共函数 setSalt($salt) { $this->salt = $salt; } // 吸气剂 /** * @return 字符串 */ 公共函数获取用户名() { 返回 $this-> 用户名; } /** * @return 字符串 */ 公共函数 getPassword() { 返回 $this->密码; } /** * @return 数组 */ 公共函数 getRoles() { 返回 $this-> 角色; } /** * @return 字符串 */ 公共函数 getSalt() { 返回 $this->salt; } // 一般的 公共函数 __construct() { $this->isActive = true; $this->salt = ''; } 公共函数 isEqualTo(UserInterface $user) { 返回 $user->getUsername() === $this->username; } 公共功能擦除凭据() { } }

test/TestBundle/Document/UserRepository.php:

命名空间测试\TestBundle\Document; 使用 Doctrine\ODM\MongoDB\DocumentRepository; 使用 Symfony\Component\Security\Core\User\UserInterface; 使用 Symfony\Component\Security\Core\User\UserProviderInterface; 使用 Symfony\Component\Security\Core\Exception\UsernameNotFoundException; 使用 Symfony\Component\Security\Core\Exception\UnsupportedUserException; 类 UserRepository 扩展 DocumentRepository 实现 UserProviderInterface { 公共函数 loadUserByUsername($username) { $q = $this->createQueryBuilder() ->field('username')->equals((string) $username) ->getQuery(); 尝试 { $user = $q->getSingleResult(); } 捕获(NoResultException $e) { throw new UsernameNotFoundException(sprintf('Can\'t find Username "%s"', $username), null, 0, $e); } 返回$用户; } 公共功能刷新用户(用户界面$用户) { $class= get_class($user); if (!$this->supportsClass($class)) { throw new UnsupportedUserException(sprintf('不支持“%s”的实例。', $class)); } 返回 $this->loadUserByUsername($user->getUsername()); } 公共函数 supportClass($class) { 返回 $class=== 'test\TestBundle\Document\User'; } }

具体route

行政: 模式:/管理员 默认值:{ _controller: testTestBundle:Test:index }

(将导致现有的控制器和视图)

预定义的user-Document 如下所示:

大批 ( [_id] => 4f59b5731c911ab41e001234 [用户名] => 测试 [密码] => 测试 [角色] => 数组 ( [0] => ROLE_ADMIN ) [盐] => [活跃] => 1 )

但我无法在/admin 使用用户名test 和密码test 登录。

【问题讨论】:

    标签: mongodb doctrine-orm symfony-2.1


    【解决方案1】:

    问题与在 apache + fastCGI 上使用 symfony 的问题有关 (https://github.com/symfony/symfony/pull/3551)。

    以上代码按预期工作。

    【讨论】:

    • 答案已过时,已修复
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-22
    • 2011-04-19
    • 2022-12-01
    • 2021-09-01
    • 2012-05-03
    • 2018-03-14
    相关资源
    最近更新 更多