2
在FOSUserBundle中,我想在用戶登錄後,在不加載頁面(AJAX查詢)的情況下將用戶重定向到fos_user_profile_show路由。我堅持在這一點上。論壇中有類似的話題,但已經過時了。Symfony 3.2 FOSUserBundle Ajax登錄
AuthenticationHandler.php
<?php
namespace AppBundle\Handler;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
/**
* Class AuthenticationHandler
* @package AppBundle\Handler
*/
class AuthenticationHandler implements AuthenticationSuccessHandlerInterface, AuthenticationFailureHandlerInterface
{
/**
* @var RouterInterface
*/
private $router;
/**
* @var Session
*/
private $session;
/**
* AuthenticationHandler constructor.
* @param RouterInterface $router
* @param Session $session
*/
public function __construct(RouterInterface $router, Session $session)
{
$this->router = $router;
$this->session = $session;
}
/**
* @param Request $request
* @param TokenInterface $token
* @return JsonResponse|RedirectResponse
*/
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
if ($request->isXmlHttpRequest()) {
return new JsonResponse(array('success' => true));
}
else {
$url = $this->router->generate('fos_user_profile_show');
return new RedirectResponse($url);
}
}
/**
* @param Request $request
* @param AuthenticationException $exception
* @return JsonResponse|RedirectResponse
*/
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
if ($request->isXmlHttpRequest()) {
return new JsonResponse(array('success' => false, 'message' => $exception->getMessage()));
} else {
$request->get('session')->set(Security::AUTHENTICATION_ERROR, $exception);
return new RedirectResponse($this->router->generate('fos_user_security_login'));
}
}
}
services.yml
app.security.authentication_handler:
class: AppBundle\Handler\AuthenticationHandler
public: false
arguments:
- "@router"
- "@session"
security.yml
firewalls:
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_token_generator: security.csrf.token_manager
check_path: fos_user_security_check
success_handler: app.security.authentication_handler
failure_handler: app.security.authentication_handler
logout: true
anonymous: true
login_content.html.twig
<script>
$(document).ready(function(){
$('#_submit').click(function(e){
e.preventDefault();
$.ajax({
type : $('form').attr('method'),
url : $('form').attr('action'),
data : $('form').serialize(),
success : function(data, status, object) {
if (data.success == false) {
console.log(data.message);
} else {
window.location.href = data.targetUrl;
}
}
});
});
</script>