登录失败后如何返回引荐来源网址? [英] How to go back to referer after login failure?
本文介绍了登录失败后如何返回引荐来源网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为成功登录,有一个参数use_referer: true
.对于登录失败,只有failure_path
,这不是我想要的.
For login success there is a parameter use_referer: true
. For login failure there is only failure_path
, which isn't what I'm looking for.
第二件事:如何做到这一点并传递错误消息?
Second thing: How to do that and pass error message?
第三件事:注销后如何返回引荐来源网址?
Third thing: How to go back to referrer after logout?
推荐答案
我解决了.
有解决方案:如何在Symfony 2中的login_check之后禁用重定向
这是解决我的问题的代码:
and here is code which solves my problem:
<?php
namespace Acme\MainBundle\Handler;
use Symfony\Component\Security\Http\Logout\LogoutSuccessHandlerInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
class AuthenticationHandler implements AuthenticationFailureHandlerInterface, LogoutSuccessHandlerInterface
{
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
$referer = $request->headers->get('referer');
$request->getSession()->setFlash('error', $exception->getMessage());
return new RedirectResponse($referer);
}
public function onLogoutSuccess(Request $request)
{
$referer = $request->headers->get('referer');
return new RedirectResponse($referer);
}
}
要处理事件,请添加到security.yml中,例如:
to handle events add to security.yml for example:
form_login:
check_path: /access/login_check
login_path: /
use_referer: true
failure_handler: authentication_handler
logout:
path: /access/logout
target: /
success_handler: authentication_handler
并转到config.yml:
and to config.yml:
services:
authentication_handler:
class: Acme\MainBundle\Handler\AuthenticationHandler
这篇关于登录失败后如何返回引荐来源网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文