gpt4 book ai didi

symfony - 从自定义身份验证处理程序调用默认处理程序

转载 作者:行者123 更新时间:2023-12-04 18:00:52 24 4
gpt4 key购买 nike

这个问题在这里已经有了答案:




9年前关闭。




Possible Duplicate:
Symfony2 AJAX Login



我已经实现了一个自定义身份验证处理程序服务来处理 AJAX 登录请求,如下所示: https://stackoverflow.com/a/8312188/267705

但是 如何处理正常的登录请求? 调用默认行为会很好,但我不知道也没有找到如何去做。

最佳答案

这是实现您想要的方法之一:

namespace YourVendor\UserBundle\Handler;

// "use" statements here

class AuthenticationHandler
implements AuthenticationSuccessHandlerInterface,
AuthenticationFailureHandlerInterface
{
private $router;

public function __construct(Router $router)
{
$this->router = $router;
}

public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
if ($request->isXmlHttpRequest()) {
// Handle XHR here
} else {
// If the user tried to access a protected resource and was forces to login
// redirect him back to that resource
if ($targetPath = $request->getSession()->get('_security.target_path')) {
$url = $targetPath;
} else {
// Otherwise, redirect him to wherever you want
$url = $this->router->generate('user_view', array(
'nickname' => $token->getUser()->getNickname()
));
}

return new RedirectResponse($url);
}
}

public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
if ($request->isXmlHttpRequest()) {
// Handle XHR here
} else {
// Create a flash message with the authentication error message
$request->getSession()->setFlash('error', $exception->getMessage());
$url = $this->router->generate('user_login');

return new RedirectResponse($url);
}
}
}

享受。 ;)

关于symfony - 从自定义身份验证处理程序调用默认处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8654265/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com