2016-06-29 21 views
1

私はたとえばsymfonyの3に、私はドキュメントのようでしたHow to Load Security Users from the Databasesymfonyの3 - 認証

エンティティユーザーとスキーマデータベースの認証をやろうとしています。

public function indexAction(Request $request) 
{ 
    $session = $request->getSession(); 

    $authenticationUtils = $this->get('security.authentication_utils'); 

    $error = $authenticationUtils->getLastAuthenticationError(); 

    $lastUsername = $authenticationUtils->getLastUsername(); 

    if ($this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')){ 
     return $this->redirect($this->generateUrl('admin_dashboard')); 
    } 

    return $this->render('default/index.html.twig', [ 
     'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..'), 
     'body_id' => "simple-page", 
     'last_username' => $lastUsername, 
     'error'   => $error 
    ]); 
} 

私はどのように与えるか: は私が_usernameを持っていると

コントローラーを_password、bcryptの、ログインフォームで平文

のsecurity.yml

security: 
    encoders: 
     AppBundle\Entity\User: plaintext 

    role_hierarchy: 
     ROLE_ADMIN:  [ROLE_USER] 

    providers: 
     our_db_provider: 
      entity: { class: AppBundle:User, property: username } 

    firewalls: 
     main: 
      pattern: ^/ 
      anonymous: ~ 
      provider: our_db_provider 

      form_login: 
       login_path:/
       check_path:/
      logout: 
       path: /logout 
       target:/

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

をMD5で使用するパスワードを試してみましたログインに間違ったデータを取得する

exception 'Symfony\Component\Security\Core\Exception\BadCredentialsException' with message 'Bad credentials.' 

私はそれを修正するので、私をadmin_dashboardにリダイレクトせず、エラーも返しません。デバッグ私はまだ匿名のままです

データベースで私はROLE_ADMINというロールを持っています。私たちはgetRolesでこれをUserエンティティに返しました。

どうしたのですか?

+2

独自の認証システムを実装することができ、あなたは例外を持っていないのですか? – goto

答えて

0

Symfonyのドキュメントを確認してください。 User EntityがUserInterfaceクラスを実装する必要があることを忘れないでください。 here you can find what you need

もあなたはそれが間違っているyou.What理解するのは難しい

class WeirdFormAuthenticator extends AbstractGuardAuthenticator 
関連する問題