単純なフォームを使用してユーザーを追加することになっているコントローラーがありますが、ユーザーを手動で認証することができません。
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()