현재 설치된 Symfony 2.1에서는 FOSUserBundle을 사용하지 않고 SecurityBundle의 Userprovider로 MongoDB를 사용할 수 있어야합니다 (여기에 소개 된 것처럼 : mongodb symfony user authentication?).symfony 2.1에서 사용자 인증을 위해 mongodb 사용하기
실제로 코드의 문제는 어디에 있는지 알 수 없지만 미리 정의 된 사용자 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
test/TestBundle/Document/User.php
-Document :
namespace test\TestBundle\Document; use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Security\Core\User\EquatableInterface; use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; /** * @ODM\Document(collection="user", repositoryClass="test\TestBundle\Document\UserRepository") */ class User implements UserInterface, EquatableInterface { /** * @ODM\Id */ protected $id; /** * @ODM\String */ protected $username; /** * @ODM\String */ protected $password; /** * @ODM\Collection */ protected $roles = array(); /** * @ODM\String */ protected $salt; /** * @ODM\Boolean */ protected $isActive; // Setter /** * @param String */ public function setUsername($username) { $this->username = $username; } /** * @param String */ public function setPassword($password) { $this->password = $password; } /** * @param String */ public function setRole($role) { $this->roles[] = $role; } /** * @param array */ public function setRoles(array $roles) { $this->roles = (array) $roles; } /** * @param String */ public function setSalt($salt) { $this->salt = $salt; } // Getter /** * @return String */ public function getUsername() { return $this->username; } /** * @return String */ public function getPassword() { return $this->password; } /** * @return array */ public function getRoles() { return $this->roles; } /** * @return String */ public function getSalt() { return $this->salt; } // General public function __construct() { $this->isActive = true; $this->salt = ''; } public function isEqualTo(UserInterface $user) { return $user->getUsername() === $this->username; } public function eraseCredentials() { } }
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'; } }
특정 route
:
Admin: pattern: /admin defaults: { _controller: testTestBundle:Test:index }
미리 정의 된 user
-Document은 다음과 같습니다 (기존 컨트롤러와보기로 이어질 것입니다) :
Array ( [_id] => 4f59b5731c911ab41e0 [username] => test [password] => test [roles] => Array ( [0] => ROLE_ADMIN ) [salt] => [isActive] => 1 )
하지만 사용자 이름 test
과 암호 test
에 로그인 할 수 없습니다 /admin
.
답변이 오래되었습니다. 수정되었습니다. – Gigala