php - Symfony 4.4 + scheb/2fa:自定义代码生成器和邮件程序
问题描述
我正在尝试遵循此文档,但 yaml 配置会引发一些错误。我对 Symfony 的了解还不够,无法理解我做错了什么。 https://github.com/scheb/2fa/blob/5.x/doc/providers/email.md
服务.yaml:
https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
uploads_path: '/uploads/files/'
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
App\:
resource: '../src/'
exclude:
- '../src/DependencyInjection/'
- '../src/Entity/'
- '../src/Kernel.php'
- '../src/Tests/'
App\Controller\:
resource: '../src/Controller/'
tags: ['controller.service_arguments']
# App\Service\CodeGeneratorService:
# arguments: ['@markdown.parser', '@doctrine_cache.providers.my_markdown_cache']
auth_mailer:
# class: Scheb\TwoFactorBundle\Mailer\SymfonyAuthCodeMailer
class: App\Mailer\MyAuthCodeMailer
code_generator_service:
class: App\Service\CodeGeneratorService
# public: true
arguments:
# $mailer: "%scheb_two_factor.security.email.symfony_auth_code_mailer%"
# $mailer: "%scheb_two_factor.security.email.swift_auth_code_mailer%"
# $mailer: App\Mailer\MyAuthCodeMailer
# $mailer: Scheb\TwoFactorBundle\Mailer\SymfonyAuthCodeMailer
# $mailer: Scheb\TwoFactorBundle\Mailer\AuthCodeMailerInterface
# $mailer: App\service\CodeGeneratorService
# $mailer: "%scheb_two_factor.email.mailer%"
$digits: "%scheb_two_factor.email.digits%"
scheb_2fa.yaml:
# See the configuration reference at https://github.com/scheb/2fa/blob/master/doc/configuration.md
scheb_two_factor:
security_tokens:
- Symfony\Component\Security\Guard\Token\PostAuthenticationGuardToken
email:
enabled: true
sender_email: quezako35@gmail.com
sender_name: Quezako # Optional
digits: 12
code_generator: code_generator_service # Use alternative service to generate authentication code
mailer: auth_mailer # Use alternative service to send the authentication code
应用\服务\CodeGeneratorService.php:
<?php
declare(strict_types=1);
namespace App\Service;
use Scheb\TwoFactorBundle\Model\PersisterInterface;
use Scheb\TwoFactorBundle\Mailer\AuthCodeMailerInterface;
use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface;
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Email\Generator\CodeGeneratorInterface;
class CodeGeneratorService implements CodeGeneratorInterface
{
/**
* @var PersisterInterface
*/
private $persister;
/**
* @var AuthCodeMailerInterface
*/
private $mailer;
/**
* @var int
*/
private $digits;
public function __construct(PersisterInterface $persister, AuthCodeMailerInterface $mailer, int $digits)
{
$this->persister = $persister;
$this->mailer = $mailer;
$this->digits = $digits;
}
public function generateAndSend(TwoFactorInterface $user): void
{
$min = 10 ** ($this->digits - 1);
$max = 10 ** $this->digits - 1;
$code = $this->generateCode($min, $max);
$user->setEmailAuthCode((string) $code);
$this->persister->persist($user);
$this->mailer->sendAuthCode($user);
}
public function reSend(TwoFactorInterface $user): void
{
$this->mailer->sendAuthCode($user);
}
protected function generateCode(int $min, int $max): string
{
return substr(md5(rand()), $min, $max);
}
}
我收到的各种错误消息:
无法自动装配服务“code_generator_service”:方法“App\Service\CodeGeneratorService::__construct()”的参数“$mailer”引用接口“Scheb\TwoFactorBundle\Mailer\AuthCodeMailerInterface”,但不存在此类服务。您可能应该将此接口别名为以下现有服务之一:“App\Mailer\MyAuthCodeMailer”、“auth_mailer”、“scheb_two_factor.security.email.swift_auth_code_mailer”、“scheb_two_factor.security.email.symfony_auth_code_mailer”。
您请求了一个不存在的参数“scheb_two_factor.email.mailer”。您的意思是其中之一:“scheb_two_factor.email.sender_name”、“scheb_two_factor.email.template”、“scheb_two_factor.email.digits”?
传给 App\Service\CodeGeneratorService::__construct() 的参数 2 必须实现接口 Scheb\TwoFactorBundle\Mailer\AuthCodeMailerInterface,给定字符串,在 D:\Dev\meet_the_team\var\cache\dev\Container0FXY0Rx\srcApp_KernelDevDebugContainer.php 中调用621
解决方案
作者 Christian Scheb 给了我答案。
您必须配置一个服务并使用 @ 注释来引用该服务,以告诉 Symfony 使用该服务实例。 https://github.com/scheb/2fa/issues/22
services:
# ...
auth_mailer:
class: App\Mailer\MyAuthCodeMailer
code_generator_service:
class: App\Service\CodeGeneratorService
arguments:
$mailer: "@auth_mailer"
$digits: "%scheb_two_factor.email.digits%"
推荐阅读
- python - ColumnTransformer 在管道中使用 CountVectorizer 失败
- javascript - 打字稿查看对象并将字符串更改为标题大小写?
- java - 我可以从 JUnit5 中的 @BeforeEach 中排除单个测试吗?
- julia - 当处理多个数组时,为什么 julia 广播不起作用?
- rally - 切换 API KEY 后的空查询结果 - CA Agile Rally API
- python - 要求两个数字的程序:一个小数(浮点数)和一个整数(整数),第一个数字(浮点数)到第二个数(int)的幂
- html - 如何设置具有定义边界的区域作为链接?
- c# - 在 C#/.NET Core 2.1 应用程序中运行 Powershell 5.1
- r - 在插入符号包中使用 createDataPartition 时出错
- php - Cron 作业 - 如何防止发送重复通知?