I finally found the answer.
$token = new \Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken($user, $pw, "main", array("ROLE_USER"));
$this->get('security.context')->setToken($token);
$event = new \Symfony\Component\Security\Http\Event\InteractiveLoginEvent($this->getRequest(), $token);
$this->get('event_dispatcher')->dispatch('security.interactive_login', $event);
$user = $this->get('security.context')->getToken()->getUser();