0

Symfony2 で動作するデータベース ログイン システムを取得しようとしています。私はドキュメントを読みましたが、何をすべきかについて完全に困惑しています...

ログインしようとするたびに、「Bad Credentials」が表示されます。

security.yml

security:
    encoders:
        D\UserBundle\Entity\User:
            algorithm:       sha512
            encode_as_base64: false
            iterations:      1

role_hierarchy:
    ROLE_ADMIN:      ROLE_USER
    ROLE_SUPER_ADMIN: [ ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH ]

providers:
    user_db:
        entity: { class: DUserBundle:User}

firewalls:
    secured_area:
        pattern:   ^/
        anonymous: ~
        form_login:
            login_path: /login
            check_path: /login_check
        logout:
            path:  /logout
            target:

access_control:
    - { path: ^/admin, roles: ROLE_ADMIN }
    - { path: ^/user, roles: ROLE_USER }

UserRepository.php

<?php

namespace D\UserBundle\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->loadUserByUsername($user->getUsername());
    }

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

ユーザー.php

UserController.php

それが私のデータベース、テーブル(ユーザー)です

id:5
username:Pawel
salt:5633267d072cd3119af5270d64b4b45d
password:test
email:test@test.pl
is_active1
4

2 に答える 2

0

security.yml は次のようになります。

providers:
        user_db:
            entity: { class: DUserBundle:User, property: username }

UserRepository クラスはまったく必要ありません。

于 2012-11-20T21:51:23.393 に答える
0

本当に遅くなって申し訳ありませんが、質問がまだ回答されていないため...

エンティティのエイリアスをプロバイダーの「クラス」値として使用できるかどうかはわかりません。

完全な名前空間を持つエンティティ名で設定してみてください:

volunteers:
    entity:
        class: D\UserBundle\Entity\User

しかし、主な問題は、データベースでパスワードを暗号化する必要があることです。少なくとも、おそらく暗号化する必要があります。security.yml 構成ファイルで定義したセキュリティ エンコーダーを使用する場合、ドキュメントによると、正しい方法は、登録フォームの検証後にエンコーダーを管理することです。

ユーザーの登録フォームがあるとします。

# Any routing file
d_userbundle_register:
    pattern: /register
    defaults:
        _controller: DUserBundle:Users:register
    requirements:
        _method: get

d_userbundle_register_check:
    pattern: /register_check
    defaults:
        _controller: DUserBundle:Users:registerCheck
    requirements:
        _method: post

そして、コントローラーの registerCheckAction:

// UsersController.php
public function registerCheckAction()
{
    $request = $this->getRequest();

    $user = new D\UserBundle\Entity\User();
    $registrationForm = $this->createForm(new D\UserBundle\Form\UserType(), $user);

    $registrationForm->bind($request);

    if ($registrationForm->isValid()) {
        $em = $this->getDoctrine()->getManager(); // use getEntityManager() instead of getManager() for old symfony2 versions

        // Get the factory encoder service
        $encoderFactory = $this->get('security_encoder.factory');
        // Retrieve the algorithms, iterations and everything defined in your security.yml file
        $encoder = $encoderFactory->getEncoder($user);
        // Encrypt the password with the user's plaintext password AND the user salt
        $encryptedPassword = $encoder->encodePassword($user->getPassword(), $user->getSalt());
        // Then set the user's encrypted password :)
        $user->setPassword($encryptedPassword);

        $em->persist($user);
        $em->flush();

        // Thanks the user
        $request->getSession()->getFlashBag()->add('success', 'Heyoh, welcome on board !');
        return $this->redirect($this->generateUrl('d_userbundle_login'));
    }

    return $this->render('DUserBundle:User:registration.html.twig', array(
        'registration_form' => $registrationForm->createView()
    ));
}
于 2013-08-27T22:10:52.927 に答える