Symfony2 getUser returns null
I came across a strange question:
I am using 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
but, i write the code in defaultController.php
$this->getUser()
returns null
and
var_dump($this->get('security.token_storage')->getToken());
object (Symfony \ Component \ Security \ Core \ Authentication \ Token \ AnonymousToken) [107]
I don't know where my logic goes wrong. Google hasn't found any issues either.
+3
source to share
No one has answered this question yet
Check out similar questions: