登录失败后如何返回referer? [英] How to go back to referer after login failure?
本文介绍了登录失败后如何返回referer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
登录成功有一个参数use_referer: true
.对于登录失败,只有 failure_path
,这不是我要找的.p>
第二件事:如何做到这一点并传递错误信息?
第三件事:退出后如何回到referrer?
解决方案
我解决了.
有解决方案:如何在 Symfony 2 中 login_check 后禁用重定向
这是解决我的问题的代码:
headers->get('referer');$request->getSession()->setFlash('error', $exception->getMessage());返回新的重定向响应($referer);}公共函数 onLogoutSuccess(Request $request){$referer = $request->headers->get('referer');返回新的重定向响应($referer);}}
处理事件添加到security.yml例如:
form_login:check_path:/access/login_check登录路径:/use_referer: 真failure_handler: authentication_handler登出:路径:/访问/注销目标:/成功处理程序:身份验证处理程序
和 config.yml:
服务:身份验证处理程序:类:AcmeMainBundleHandlerAuthenticationHandler
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?
解决方案
I solved it.
There is solution: How to disable redirection after login_check in Symfony 2
and here is code which solves my problem:
<?php
namespace AcmeMainBundleHandler;
use SymfonyComponentSecurityHttpLogoutLogoutSuccessHandlerInterface;
use SymfonyComponentSecurityHttpAuthenticationAuthenticationFailureHandlerInterface;
use SymfonyComponentSecurityCoreExceptionAuthenticationException;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationRedirectResponse;
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);
}
}
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
and to config.yml:
services:
authentication_handler:
class: AcmeMainBundleHandlerAuthenticationHandler
这篇关于登录失败后如何返回referer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文