我有一个控制器,它应该通过一个简单的表单添加用户,但我无法让用户进行手动身份验证。
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken
public function addAction($user)
{
$token =new UsernamePasswordToken(
$user->getUsername(),
$user->getPassword(),
'secured_area',
$user->getRoles()
);
$this->get('security.context')->setToken($token);
// as suggested in some other answers
$request->getSession()->set('_security_secured_area', serialize($token));
// as suggested in http://techblog.zabuchy.net/2012/manually-authenticate-symfony-2-user/
return $this->redirect($this->generateUrl('acme_project_secure_show' )
);
}
}
重定向到安全路由有效,但是由于身份验证设置不$this->getUser()
正确,该 方法返回
null ...$user= $this->get('security.context')->getToken();
$user= $this->get('security.context')->getToken()->getUser();
$this->getUser()