1
如何獲得成功授權後返回的json對象,而不是重定向到默認路徑?我使用的是Symfony 3.0.1,我沒有使用FOSBundle。如何在成功授權後返回json對象而不是重定向
我的登錄控制器看起來是這樣的:
class ClientLoginController extends Controller
{
/**
* @Route("/login", name="login")
*/
public function loginAction(Request $request)
{
$client = new Client();
$form = $this->createForm(ClientLoginType::class, $client);
$form->handleRequest($request);
$authenticationUtils = $this->get('security.authentication_utils');
$lastEmail = $authenticationUtils->getLastUsername();
$error = $authenticationUtils->getLastAuthenticationError();
if ($form->isSubmitted() && $form->isValid())
{
return new JsonResponse(
array(
'message' => 'Success! You're authorised!',
'result' => $this->renderView('SymfonyBundle::client/success.html.twig')
), 200);
}
return $this->render(
'SymfonyBundle::security/security.html.twig',
array(
'login_form' => $form->createView(),
'error' => $error,
'last_email' => $lastEmail,
)
);
}
}
而且在security.yml配置登錄部分看起來是這樣的:
form_login:
login_path: login
check_path: login
username_parameter: _email
failure_forward: false
default_target_path: login
csrf_token_generator: security.csrf.token_manager
預先感謝您!
非常感謝您!創建一個身份驗證處理程序似乎是全面的:) – rvaliev
@rvaliev如果你找到這個答案是你正在尋找的,請接受它:) – tftd