每个防火墙使用不同的模板
你在多个防火墙上使用双因素身份验证,并且需要为每个防火墙以不同的方式渲染表单。这是一个基本解决方案供你参考
像这样创建一个新的表单渲染器类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
<?php
namespace Acme\Demo;
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\TwoFactorFormRendererInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Http\FirewallMapInterface;
use Twig\Environment;
class CustomFormRenderer implements TwoFactorFormRendererInterface
{
public function __construct(
private Environment $twigEnvironment,
private FirewallMapInterface $firewallMap,
private array $templates, // Map of [firewall name => template path]
) {
}
public function renderForm(Request $request, array $templateVars): Response
{
$firewallName = $this->firewallMap->getFirewallConfig($request)->getName();
$template = $this->templates[$firewallName];
$content = $this->twigEnvironment->render($template, $templateVars);
$response = new Response();
$response->setContent($content);
return $response;
}
}
将其注册为服务
1 2 3 4 5 6 7 8 9
# config/services.yaml
services:
acme.custom_form_renderer:
class: Acme\Demo\CustomFormRenderer
arguments:
- '@twig'
- '@security.firewall.map'
# This is a map of firewall name to template path
- { main: 'security/2fa_google.html.twig', admin: 'admin/security/2fa_google.html.twig' } ]
将新服务配置为表单渲染器
1 2 3 4 5
# config/packages/scheb_2fa.yaml
scheb_two_factor:
google: # Or "totp" or "email", depending on the two-factor provider you're using
enabled: true
form_renderer: acme.custom_form_renderer
本作品,包括代码示例,根据 Creative Commons BY-SA 3.0 许可协议获得许可。