symfony2 getUser返回null

时间:2015-07-22 08:17:08

标签: symfony null return

我遇到了一个奇怪的问题:

我使用vesion 2.7,

PHP 5.4.41-0 + deb7u1

security.yml:

services:
     webservice_user_provider:
         class: AppBundle\Security\User\WebserviceUserProvider
         arguments:    [@service_container]

security:
    encoders:
        AppBundle\Entity\User:
            algorithm: sha512

    providers:
        webservice:
            id: webservice_user_provider

        in_memory:
            memory: ~

    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt|error)|css|images|js)/
            security: false

        main:
            pattern:    /.*
            form_login:
                login_path: login
                check_path: login_check
                use_referer: true
            remember_me:
                key: "%secret%"
                lifetime: 31536000
                path: /
                domain: ~
            logout:
                path:   logout
            anonymous: true
    access_control:
        # require ROLE_ADMIN for /admin*
        - { path: ^/admin, roles: ROLE_SUPER_ADMIN }
        - { path: /.*, role: IS_AUTHENTICATED_ANONYMOUSLY }

baseController.php:

  private function auth($email){
         $user=$this->getDoctrine()->getRepository('AppBundle:User')->getByUserEmail($email);

         $token = new UsernamePasswordToken($user,null,'main',$user->getRoles());

         $this->get('security.token_storage')->setToken($token);

         return $this->get('security.token_storage')->getToken()->getUser();
   }
  

$ userToken = $ this-> auth(' admin@examole.com');

     

($ userToken instanceof User)== true

但是,我在defaultController.php中编写代码

$this->getUser()

  

返回null

var_dump($this->get('security.token_storage')->getToken());
  

对象(Symfony的\元器件\安全\核心\认证\令牌\ AnonymousToken)[107]

我不知道我的逻辑错在哪里。 谷歌也没有发现任何问题。

0 个答案:

没有答案