【问题标题】:Authentication Symfony2 from email link [closed]来自电子邮件链接的身份验证 Symfony2 [关闭]
【发布时间】:2012-07-04 08:43:06
【问题描述】:

您知道通过电子邮件链接在 Symfony2 平台上验证用户的技巧吗?

非常感谢之前

【问题讨论】:

  • 你做了什么?您的问题太笼统了,如果没有更多规范,很难回答。

标签: php authentication symfony


【解决方案1】:

您可能想查看FOSUserBundle,它内置了很多用于管理用户的内容。

我们使用类似的技术来注册用户。这是一些基本代码(您需要填写错误处理以及根据 URL 参数实际找到用户的方法:

/**
 * @Route("/register/activate/{hash}/{oId}", requirements={"hash"="\w+", "oId"="\d+"}, name="register_byhash")
 * @Method({"GET"})
 * @Template()
 */
public function registerByHashAction($hash, $oId)
{
    $um = $this->container->get('fos_user.user_manager');
    $user = $um->findUserByHash($hash, $oId); // You will need to supply a method that finds the user and checks the hash

    // Mark the user as now active and save the user here

    $providerKey = $this->container->getParameter('fos_user.firewall_name');
    $token = new UsernamePasswordToken($user, null, $providerKey, $user->getRoles());
    $this->container->get('security.context')->setToken($token);


    $url = $this->container->get('router')->generate('welcome');
    return new RedirectResponse($url);
}

【讨论】:

    【解决方案2】:

    大概是这样的:

    Pesudocode:
    
    generate hashed link that is unique to user (e.g. md5(email+timestamp+salt) )
    store hash for user in db
    Send link to user in email (same email as used in hash)
    
    when user accesses site using link:
    
    fetch hash part from link
    match hash against database
    if match is found, authenticate, else fail
    

    请注意,我不保证这是一种安全的方式(不知道仅通过链接进行身份验证时是否可能是安全的)。只是一些想法可以帮助您前进。

    【讨论】:

      【解决方案3】:

      解决此问题的最佳方法是实施自定义身份验证提供程序。身份验证提供程序负责用户身份验证,使用您想要的几乎所有内容:可以是 cookie(例如记住我)、表单数据,或者在您的情况下是查询字符串参数。

      你需要:

      • 用于存储身份验证参数的自定义令牌
      • 可以管理令牌和对用户进行身份验证的自定义身份验证提供程序
      • 一些胶水将这些组件连接到安全系统(意味着您需要定义工厂和一些配置)。

      使用身份验证提供程序,您将有机会将登录用户分配给自定义安全角色(就像记住我功能一样),因此您将能够区分登录用户和“自动登录”用户。如果您想限制自动登录的用户可以执行的操作(例如,您可能希望使身份验证仅对一项特定操作有效),这可能非常有用。

      官方食谱中有一个关于自定义身份验证提供程序的秘诀:

      http://symfony.com/doc/current/cookbook/security/custom_authentication_provider.html

      我已经做了很多你需要做的事情,并在 screenfony.com 博客上写了这篇文章:

      http://www.screenfony.com/blog/symfony-custom-authentication-provider

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-05-20
        • 2018-09-10
        • 1970-01-01
        • 2018-12-08
        • 2018-02-10
        • 1970-01-01
        • 2020-06-22
        • 2023-03-18
        相关资源
        最近更新 更多