简体   繁体   中英

Generate Authentication token and authenticate as a user manually in a controller

I am using Symfony2 and FOSUserBundle . I want to get a user with Doctrine ORM then logging in as it.

$user = $em->getRepository("InoUserBundle:User")->find(10);

// I want the security context as $user

like so:

$token = new UsernamePasswordToken($user, '12345', "admin", $user->getRolesAsArray());
$this->get('security.context')->setToken($token);

$event = new InteractiveLoginEvent($request, $token);
$this->get("event_dispatcher")->dispatch("security.interactive_login", $event);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM