0

我的 security.yml 是

# app/config/security.yml
security:
    encoders:
       Iweb\IndexBundle\Entity\User:
           algorithm:        sha1
           encode_as_base64: false
           iterations:       1


role_hierarchy:
    ROLE_ADMIN:       ROLE_ADMIN

providers:
    administrators:
        entity: { class: IwebIndexBundle:User, property: username }

firewalls:
    login:
        pattern:  ^/login$
        security: false
    secured_area:
        pattern:    ^/(admin/*|login_check)
        anonymous: false
        form_login:
            login_path: /login
            check_path: /login_check
            always_use_default_target_path: true
            default_target_path:            /admin/panel
            target_path_parameter:          _target_path
            use_referer:                    false
            username_parameter:             _username
            password_parameter:             _password
            csrf_parameter:                 login[_token]
        logout: 
            path: /admin/logout
            target: /login
access_control:
    - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/admin, roles: ROLE_ADMIN }

和实体 user.php 是

 <?php
 // src/Iweb/IndexBundle/Entity/AcmeUsers.php
  namespace Iweb\IndexBundle\Entity;

 use Doctrine\ORM\Mapping as ORM;
 use Symfony\Component\Security\Core\User\UserInterface;
  use Symfony\Component\Security\Core\User\EquatableInterface;
/**
 * Iweb\IndexBundle\Entity\User
 *
 * @ORM\Table(name="acme_users")
 * @ORM\Entity(repositoryClass="Iweb\IndexBundle\Entity\UserRepository")
 */
class User implements UserInterface, \Serializable
{
/**
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORM\Column(type="string", length=25, unique=true)
 */
private $username;

/**
 * @ORM\Column(type="string", length=32)
 */
private $salt;

/**
 * @ORM\Column(type="string", length=40)
 */
private $password;

/**
 * @ORM\Column(type="string", length=60, unique=true)
 */
private $email;

/**
 * @ORM\Column(name="is_active", type="boolean")
 */
private $isActive;

public function __construct()
{
    $this->isActive = true;
    $this->salt = md5(uniqid(null, true));
}

/**
 * @inheritDoc
 */
public function getUsername()
{
    return $this->username;
}

/**
 * @inheritDoc
 */
public function getSalt()
{
    return $this->salt;
}

/**
 * @inheritDoc
 */
public function getPassword()
{
    return $this->password;
}

/**
 * @inheritDoc
 */
public function getRoles()
{
    return array('ROLE_ADMIN');
}

/**
 * @inheritDoc
 */
public function eraseCredentials()
{
}

/**
 * @inheritDoc
 */
public function isEqualTo(UserInterface $user)
{
    return $this->id === $user->getId();
}

/**
 * @see \Serializable::serialize()
 */
public function serialize()
{
    return serialize(array(
        $this->id,
    ));
}

/**
 * @see \Serializable::unserialize()
 */
public function unserialize($serialized)
{
    list (
        $this->id,
    ) = unserialize($serialized);
}
}

UserRepository.php 是

<?php 
        // src/Iweb/IndexBundle/Entity/UserRepository.php
        namespace Iweb\IndexBundle\Entity;

        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;
        use Doctrine\ORM\EntityRepository;
        use Doctrine\ORM\NoResultException;

        class UserRepository extends EntityRepository implements UserProviderInterface
        {
        public function loadUserByUsername($username)
        {
            $q = $this
                ->createQueryBuilder('u')
                ->where('u.username = :username OR u.email = :email')
                ->setParameter('username', $username)
                ->setParameter('email', $username)
                ->getQuery()
            ;

            try {
                // The Query::getSingleResult() method throws an exception
                // if there is no record matching the criteria.
                $user = $q->getSingleResult();
            } catch (NoResultException $e) {
                throw new UsernameNotFoundException(sprintf('Unable to find an active admin AcmeUserBundle:User object identified by "%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->find($user->getId());
        }

        public function supportsClass($class)
        {
            return $this->getEntityName() === $class || is_subclass_of($class, $this->getEntityName());
        }
    }

如果我使用 in_memory 它工作正常,但是当我从 db 加载用户时,就会出现 Bad Credentials 错误。我不想使用 fosuserbundle --- 我正在关注 symfony 文档。 http://symfony.com/doc/current/cookbook/security/entity_provider.html 我会感谢你...

4

0 回答 0