在symfony 2.1中使用mongodb进行用户身份验证
在Symfony 2.1的当前版本中,应该可以使用MongoDB作为SecurityBundle的用户提供程序,而无需使用FOSUserBundle(此处介绍:) 无法找出代码中的实际问题在哪里,但我无法使用预定义的用户登录在symfony 2.1中使用mongodb进行用户身份验证,mongodb,doctrine-orm,symfony-2.1,Mongodb,Doctrine Orm,Symfony 2.1,在Symfony 2.1的当前版本中,应该可以使用MongoDB作为SecurityBundle的用户提供程序,而无需使用FOSUserBundle(此处介绍:) 无法找出代码中的实际问题在哪里,但我无法使用预定义的用户登录test:test 我的security.yml如下所示: security: encoders: test\TestBundle\Document\User: plaintext providers: document_mem
test:test
我的security.yml
如下所示:
security:
encoders:
test\TestBundle\Document\User: plaintext
providers:
document_members:
mongodb: { class: testTestBundle:User, property: username }
firewalls:
secured_area:
pattern: ^/
http_basic:
realm: "Login to TEST"
access_control:
- { path: ^/admin, roles: ROLE_ADMIN }
role_hierarchy:
ROLE_ADMIN: ROLE_USER
Array
(
[_id] => 4f59b5731c911ab41e001234
[username] => test
[password] => test
[roles] => Array
(
[0] => ROLE_ADMIN
)
[salt] =>
[isActive] => 1
)
test/TestBundle/Document/UserRepository.php
:
namespace test\TestBundle\Document;
use Doctrine\ODM\MongoDB\DocumentRepository;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
class UserRepository extends DocumentRepository implements UserProviderInterface
{
public function loadUserByUsername($username)
{
$q = $this->createQueryBuilder()
->field('username')->equals((string) $username)
->getQuery();
try
{
$user = $q->getSingleResult();
}
catch (NoResultException $e)
{
throw new UsernameNotFoundException(sprintf('Can\'t find Username "%s"', $username), null, 0, $e);
}
return $user;
}
public function refreshUser(UserInterface $user)
{
$class = get_class($user);
if (!$this->supportsClass($class)) {
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', $class));
}
return $this->loadUserByUsername($user->getUsername());
}
public function supportsClass($class)
{
return $class === 'test\TestBundle\Document\User';
}
}
(将导致现有控制器和视图)
预定义的用户
-文档如下所示:
security:
encoders:
test\TestBundle\Document\User: plaintext
providers:
document_members:
mongodb: { class: testTestBundle:User, property: username }
firewalls:
secured_area:
pattern: ^/
http_basic:
realm: "Login to TEST"
access_control:
- { path: ^/admin, roles: ROLE_ADMIN }
role_hierarchy:
ROLE_ADMIN: ROLE_USER
Array
(
[_id] => 4f59b5731c911ab41e001234
[username] => test
[password] => test
[roles] => Array
(
[0] => ROLE_ADMIN
)
[salt] =>
[isActive] => 1
)
排列
(
[\u id]=>4f59b5731c911ab41e001234
[用户名]=>测试
[密码]=>测试
[角色]=>数组
(
[0]=>角色管理
)
[盐]=>
[isActive]=>1
)
但是我不能在
/admin
上使用用户名test
和密码test
登录问题与在apache+fastCGI上使用symfony有关(https://github.com/symfony/symfony/pull/3551)
上述代码按预期工作。答案已过时,已修复