更新到 symfony 4.1 时出现双重身份验证错误

Two factor Authentication error on update to symfony 4.1

这是我的代码示例

/**
 * @Route("/two/factor", name="google-authenticator")      
 */
public function twoFactorAction(Request $request)
{
    $user = $this->getUser();
    $secret = $this->container->get("scheb_two_factor.security.google_authenticator")->generateSecret();
    $user->setGoogleAuthenticatorSecret($secret);
    $url = null;
    if(!empty($user->getGoogleAuthenticatorSecret())){
        $url = $this->container->get("scheb_two_factor.security.google_authenticator")->getUrl($user);
    }

这里是错误

The "scheb_two_factor.security.google_authenticator" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead.

它工作正常。但是从 symfony 3.4 更新到 4.1 后,我收到了这个错误。请让我知道如何快速修复它。

您不能再使用 from 容器,因为访问修饰符。尝试使用自动装配。

你可以使用这个接口GoogleAuthenticatorInterface

它来自您当前使用的同一个包:Scheb 并拥有这个命名空间:

Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Google\GoogleAuthenticatorInterface;

您的方法如下所示:

public function index(GoogleAuthenticatorInterface $twoFactor)
{
    // ...
    $secret = $twoFactor->generateSecret();
}

如果我不清楚,我希望这个页面可以帮助你多一点。 generating secret

如前所述,Symfony 4.1 允许您将自动装配的服务注入您的控制器操作和其他地方。但是,您使用的是 scheb/two-factor 库的旧 (2.x) 版本,它没有定义自动装配服务。

由于您无法升级到此库的 4.x,因此您的选择有些受限。但是,应该可以添加 Compiler Pass 来根据您的需要修改 scheb/two-factor 库的配置。

简而言之,Compiler Pass 应该允许您通过执行以下操作将此服务重新定义为 public:

$container 
    ->getDefinition('scheb_two_factor.security.google_authenticator')
    ->setPublic(true);

您必须参考 documentation 以了解如何将编译器传递到您的应用程序。