首页 > 解决方案 > 更新到 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);
    }

这是错误

编译容器时,“scheb_two_factor.security.google_authenticator”服务或别名已被删除或内联。您应该将其公开,或者直接停止使用容器并改用依赖注入。

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

标签: phpsymfonytwo-factor-authentication

解决方案


由于访问修饰符,您不能再从容器中使用。尝试使用自动装配。

你可以使用这个接口GoogleAuthenticatorInterface

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

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

您的方法如下所示:

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

如果我不清楚,我希望这个页面可以帮助你更多。生成秘密


推荐阅读