我正在使用Symfony 2.3 LTS,我认为这与最新版本略有不同(对于我的部分问题)。
我需要覆盖'security.authentication.listener.form'服务,这是这个类:https://github.com/symfony/Security/blob/2.3/Http/Firewall/UsernamePasswordFormAuthenticationListener.php
我只想添加一些代码,没什么大不了的。
这些是重要声明的部分(在Symfony配置文件中):
<parameter key="security.authentication.listener.form.class">Symfony\Component\Security\Http\Firewall\UsernamePasswordFormAuthenticationListener</parameter>
<service id="security.authentication.listener.form"
class="%security.authentication.listener.form.class%"
parent="security.authentication.listener.abstract"
abstract="true">
</service>
<service id="security.authentication.listener.abstract" abstract="true" public="false">
<tag name="monolog.logger" channel="security" />
<argument type="service" id="security.context" />
<argument type="service" id="security.authentication.manager" />
<argument type="service" id="security.authentication.session_strategy" />
<argument type="service" id="security.http_utils" />
<argument />
<argument type="service" id="security.authentication.success_handler" />
<argument type="service" id="security.authentication.failure_handler" />
<argument type="collection"></argument>
<argument type="service" id="logger" on-invalid="null" />
<argument type="service" id="event_dispatcher" on-invalid="null" />
</service>
还有两个重点:
(1) I only have experience using Yaml and although it shouldn't be difficult converting this, it does add an additional obstacle to deal with. I will also use Yaml for my finished solution. I've never seen the on-invalid attribute though for a start.
(2) I need to pass in some additional parameters of my own.
我尝试重写类名和基本的类扩展,看看它是否正常工作,但我认为没有使用任何传入的值:
YAML:
parameters:
security.authentication.listener.form.class: MyBundle\Security\MyCustomUsernamePasswordFormAuthenticationListener
PHP类:
class MyCustomUsernamePasswordFormAuthenticationListener extends UsernamePasswordFormAuthenticationListener
{
/**
* {@inheritdoc}
*/
public function __construct(SecurityContextInterface $securityContext, AuthenticationManagerInterface $authenticationManager, SessionAuthenticationStrategyInterface $sessionStrategy, HttpUtils $httpUtils, $providerKey, AuthenticationSuccessHandlerInterface $successHandler, AuthenticationFailureHandlerInterface $failureHandler, array $options = array(), LoggerInterface $logger = null, EventDispatcherInterface $dispatcher = null, CsrfProviderInterface $csrfProvider = null)
{
parent::__construct($securityContext, $authenticationManager, $sessionStrategy, $httpUtils, $providerKey, $successHandler, $failureHandler, $options, $logger, $dispatcher, $csrfProvider);
}
/**
* {@inheritdoc}
*/
protected function attemptAuthentication(Request $request)
{
parent::attemptAuthentication($request);
}
}
另外,我不明白为什么'security.authentication.listener.abstract'服务中的参数5为空,但如果该类为空(但不是),则该类将抛出错误。
另外,我没有在安全配置(http://symfony.com/doc/2.3/reference/configuration/security.html)中看到security.authentication.listener.form服务作为选项。如果不是,我可以像上面提到的那样覆盖,但如果可能的话,最好在security.yml中声明它。
那么在Yaml中做到这一点的最佳做法是什么?我可能会破解它,剪切和粘贴等等,但理想情况下我不需要重新声明所有参数,因为它们已经被声明。
答案 0 :(得分:3)
首先,回答为什么在安全配置(http://symfony.com/doc/2.3/reference/configuration/security.html)中看不到security.authentication.listener.form服务的原因:
现在解决您的主要问题,解决方案取决于您尝试做的事情:
如果没有,你可以这样做: 到您的services.yml添加:
acme.security.authentication.listener.form:
class: %security.authentication.listener.form.class%
parent: security.authentication.listener.abstract
abstract: true
然后create a CompilerPass inside your Bundle并将其添加到其中:
public function process(ContainerBuilder $container)
{
$definition = new DefinitionDecorator( 'acme.security.authentication.listener.form' );
$definition->setClass( 'Acme\Bundle\MyCustomUsernamePasswordFormAuthenticationListener' );
$definition->setAbstract( true );
// Here you can add your additional data
$definition->setMethodCalls($callsArray);
$container->setDefinition( 'security.authentication.listener.form', $definition );
}
只有在SecurityBundle之后启动捆绑包时,您才可以在Resources / config / services.yml内覆盖security.authentication.listener.form
而不使用compilerpass。但这是一种我不推荐的做法。使用CompilerPass时,您将始终确保它在所有捆绑包初始化后运行。
希望这有帮助