【问题标题】:Authenticate anonymously with roles使用角色进行匿名身份验证
【发布时间】:2021-11-19 20:00:50
【问题描述】:

我有一个需要匿名验证用户身份的身份验证器,但包含一个角色。我通过在 Authenticator 中覆盖 createAuthenticatedToken 来做到这一点:

class ClientAuthenticator extends AbstractGuardAuthenticator
{
    // supports(), getCredentials(), all working

    public function getUser($credentials, UserProviderInterface $userProvider)
    {
        return new SessionUser;
    }

    // Return an anonymous user with the client role
    public function createAuthenticatedToken(UserInterface $user, $providerKey)
    {
        return new AnonymousToken(
            'Ynpir6i',                // <-- here's the issue (the $secret param)
            'anon.',
            ['ROLE_CLIENT_GUEST']
        );
    }
}

这非常有效——当我硬编码 AnonymousToken 的“秘密”参数时。

我不知道如何动态获取这个秘密。它不是parameters.yml(又名%kernel.secret%)中提供的“秘密”参数。

我只是通过在AnonymousAuthenticationListener 中设置时将其转储出来才得到我现在使用的秘密。我查看了该服务的配置,但根本没有设置。

这个秘密参数是什么,如何将它注入到我的身份验证器中?

或者,是否有更好的方法将角色添加到以特定方式进行身份验证的匿名令牌?

【问题讨论】:

    标签: symfony authentication symfony-3.4 symfony-guard


    【解决方案1】:

    该参数可以设置为security.yml中的已知值:

    security:
        firewalls:
            main:
                anonymous:
                    secret: '%secret%'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-28
      • 1970-01-01
      • 2018-05-25
      • 1970-01-01
      • 1970-01-01
      • 2020-05-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多