0

我有一个需要匿名验证用户的身份验证器,但包含一个角色。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。我查看了该服务的配置,但根本没有看到它设置。

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

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

4

1 回答 1

0

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

security:
    firewalls:
        main:
            anonymous:
                secret: '%secret%'
于 2021-09-27T18:20:49.813 回答