更新到Symfony 4.1时出现两因素身份验证错误

时间:2018-10-27 18:55:29

标签: php symfony two-factor-authentication

这是我的代码示例

/**
 * @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后,出现此错误。请让我知道如何快速修复它。

2 个答案:

答案 0 :(得分:0)

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

您可以使用此界面 GoogleAuthenticatorInterface

它来自与您当前使用的相同的软件包:Scheb 并拥有以下名称空间:

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

您的方法如下:

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

如果我不清楚,希望此页面可以为您提供更多帮助。 generating secret

答案 1 :(得分:0)

如前所述,Symfony 4.1允许您将自动连线的服务注入到控制器动作和其他地方。但是,您正在使用int64_t库的较旧(2.x)版本,该版本未定义自动装配服务。

由于您无法升级到该库的4.x版本,因此您的选择受到一定限制。但是,应该可以添加Compiler Pass来修改scheb/two-factor库的配置以满足您的需求。

简而言之,通过编译器通行证,您可以通过执行以下操作将此服务重新定义为公共服务:

scheb/two-factor

您必须参考documentation,以了解如何在应用程序中实现编译器传递。