首页 > 解决方案 > 使用角色进行匿名身份验证

问题描述

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

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 (aka %kernel.secret%) 中提供的“秘密”参数。

我只是通过在设置时将其倾倒而得到了我现在使用的秘密AnonymousAuthenticationListener。我查看了该服务的配置,但根本没有看到它设置。

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

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

标签: symfonyauthenticationsymfony-3.4symfony-guard

解决方案


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

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

推荐阅读