在symfony2登录成功之后进行重定向? [英] Do something just after symfony2 login success and before redirect?
问题描述
我正在寻找一段时间,任何信息,在symfony2身份验证成功后如何做一些事情。我想重新使用用户密码才能使用bcrypt,使用旧的哈希成功验证后。我需要这样做,当我仍然有有效的纯密码,所以它应该只是在凭据检查和重定向之前。
任何线索如何实现?
我发现了Symfony中的事件调度程序,但是我找不到成功验证后是否有任何事件。
如果我尝试以这种错误的方式提出一些更好的方法,请更正我。
// 编辑
确定我发现事件在验证成功后,它被称为 security.authentication.success
。所以我现在可以附加到这个事件,但现在我不知道我的边界代码应该附加我的事件监听器?应该在 load()
方法中的 /src/Pkr/BlogUserBundle/DependencyInjection/PkrBlogUserExtension.php
中执行此操作?
您可以指定登录成功处理程序在成功登录时执行。
例如,您的security.yml
防火墙:
main:
pattern:^ /
form_login:
success_handler:my.security.login_handler
现在创建实现 Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface
的类,并且成功登录后,你可以做任何你需要的,并处理重定向,你认为合适。
/ **
*
* /
public function onAuthenticationSuccess(Request $ request,TokenInterface $ token)
{
//处理并返回响应
}
然后在您的bundle的services.xml中创建一个具有该名称的服务,或者在使用新创建的处理程序的config.yml中创建一个服务。
我最初在本教程中找到了如何做到这一点:
http://www.reecefowell.com/2011/10/26/redirecting-on-loginlogout-in-symfony2 -using-loginhandlers /
I'm searching for a while now, for any info, on how to do something after authentication success in symfony2. I want to rehash user password to use bcrypt just after successful authentication using old hash. I need to do this when I still have valid plain password so it should be just after credentials check and before redirect.
Any clues how to achieve that?
I found something about event dispatcher in Symfony but I can't find if there is any event after successful authentication.
Please correct me if I'm trying to do this wrong way and suggest some better approach.
//EDIT
Ok I found event fired just after auth success, it's called security.authentication.success
. So i can now attach to this event but now I'm not sure where in my boundle code should I attach my event listener? Should I do that in my /src/Pkr/BlogUserBundle/DependencyInjection/PkrBlogUserExtension.php
in load()
method?
You can specify a login success handler to be executed on successful login.
For example, your security.yml
firewalls:
main:
pattern: ^/
form_login:
success_handler: my.security.login_handler
Now create the class which implements Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface
and on successful login, you can do whatever you need and handle the redirect as you see fit.
/**
*
*/
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
// handle it and return a response
}
Then create a service with that name in your services.xml for your bundle, or in your config.yml using the newly created handler.
I originally found out how to do this following this tutorial:
http://www.reecefowell.com/2011/10/26/redirecting-on-loginlogout-in-symfony2-using-loginhandlers/
这篇关于在symfony2登录成功之后进行重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!