如何在 Symfony 2 中配置记住我的监听器? [英] How to configure a remember me aware listener in Symfony 2?

查看:20
本文介绍了如何在 Symfony 2 中配置记住我的监听器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功实施了自定义身份验证提供程序,但现在我还需要添加记住我"功能,但我找不到有关如何执行此操作的文档.

I have implemented a custom authentication provider successfully, but now I also need to add 'remember me' functionality, and I couldn't find docs on how to do that.

我尝试添加:

remember_me:
    key: "%secret%"
    lifetime: 31536000 # 1 year
    always_remember_me: true

但它是这样写的:

您必须为每个启用了记住我的防火墙配置至少一个记住我的监听器(例如表单登录).

我找到了这个,但我不知道如何使用它:Symfony\Component\Security\Core\Authentication\Provider\RememberMeAuthenticationProvider

I found this but I'm not sure how to use it: Symfony\Component\Security\Core\Authentication\Provider\RememberMeAuthenticationProvider

那么 RememberMeAwareInterface 在哪里?(我猜有一个?像 ContainerAware)我该怎么办?

So where is the RememberMeAwareInterface? (I guess there is one? Like ContainerAware) And what should I do with it?

我认为我不需要编写自己的实现,默认实现应该可以与我的自定义身份验证提供程序一起正常工作.

I don't think I need to write my own implementation, the default one should work fine with my custom auth provider.

推荐答案

我在使用我编写的自定义 Facebook 身份验证提供程序时遇到了同样的问题.解决方案最终非常简单:

I was having the same issue with a custom Facebook authentication provider I wrote. The solution ended up being pretty simple:

我假设您使用自定义的 SecurityFactoryInterface 实现实现了自定义身份验证提供程序,该实现从 Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\AbstractFactory 扩展而来.如果你这样做了,剩下的就是配置问题了:

I'll assume you implemented a custom authentication provider with a custom SecurityFactoryInterface implementation that extends from Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\AbstractFactory. If you did this, the rest is a matter of configuration:

  1. 在您的安全配置中,为您的防火墙配置 remember_me 功能.假设您将其配置到 public 防火墙中,添加的配置参数可能如下所示:

  1. In your security configuration, configure the remember_me functionality for your firewall. Assuming you're configuring that into the public firewall, the added config params might look something like this:

firewalls:
    public:
        remember_me:
            key:      "%secret%"
            lifetime: 31536000 # 365 days in seconds
            path:     /
            domain:   ~ # Defaults to the current domain from $_SERVER

  • 在相同的配置中,为您的身份验证提供程序启用 remember_me 功能.假设您将其配置到 public 防火墙中,并且您的 SecurityFactoryInterface 实现的 getKey() 方法返回 yourAuthProviderKey,添加的配置参数可能如下所示:

  • In the same configuration, enable the remember_me functionality for your authentication provider. Assuming you're configuring that into the public firewall and your SecurityFactoryInterface implementation's getKey() method returns yourAuthProviderKey, the added config params might look something like this:

    firewalls:
        public:
            yourAuthProviderKey:
                remember_me:        true
    

  • 最后,当您的身份验证提供程序处理登录时,请确保您使用名为 _remember_me 且值为 1 的 http GET 或 POST 参数来请求记住我功能在 http 请求中.(但请注意:如果您在安全配置中更改了其默认值,则此参数可能需要不同的名称.)例如,就我而言,我不得不告诉 Facebook 在处理身份验证后重定向到以下 URL:http://www.mydomain.com/auth-callback/?_remember_me=1.(注意之后的部分?)

  • Finally, when your Authentication Provider handles logins, make sure you request the remember me feature by having an http GET or POST parameter named _remember_me with value 1 in the http request. (Note though: this parameter might need a different name if you changed its default value in your security config.) For example, in my case, I had to tell Facebook to redirect to the following URL after it handled the authentication: http://www.mydomain.com/auth-callback/?_remember_me=1. (Note the part after the ?)

    希望这会有所帮助!

    这篇关于如何在 Symfony 2 中配置记住我的监听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆