我已创建自定义身份验证。但由于我未知的原因,有两个活动的身份验证侦听器。因此身份验证提供程序管理器称为Twice。
第一次调用AuthListener
,它运行mu AuthenticationProviderManager
,返回正确的令牌。然后调用UsernamePasswordFormAuthenticationListener
再次运行AuthenticationProviderManager
。它会返回“不良凭证”。
如何“停用”UsernamePasswordFormAuthenticationListener
?所以只调用我的自定义监听器?
安全性:
providers:
api:
id: api_user_provider
firewalls:
secured_area:
pattern: ^/
user: true
form_login:
login_path: /login
check_path: /check_login
logout: ~
服务:
api_user_provider:
class: Ad\FrontendBundle\Security\User\UserProvider
arguments: ["@service_container"]
user.security.authentication.provider:
class: Ad\FrontendBundle\Security\Authentication\Provider\AuthProvider
arguments: ['', %kernel.cache_dir%/security/nonces]
user.security.authentication.listener:
class: Ad\FrontendBundle\Security\Firewall\AuthListener
arguments: ["@security.context", "@security.authentication.manager"]
厂
class ApiFactory implements SecurityFactoryInterface
{
public function create(ContainerBuilder $container, $id, $config, $userProvider, $defaultEntryPoint)
{
$providerId = 'security.authentication.provider.user.'.$id;
$container
->setDefinition($providerId, new DefinitionDecorator('user.security.authentication.provider'))
->replaceArgument(0, new Reference($userProvider))
;
$listenerId = 'security.authentication.listener.user.'.$id;
$listener = $container->setDefinition($listenerId, new DefinitionDecorator('user.security.authentication.listener'))
;
return array($providerId, $listenerId, $defaultEntryPoint);
}
public function getPosition()
{
return 'pre_auth';
}
public function getKey()
{
return 'user';
}
public function addConfiguration(NodeDefinition $node)
{}
}
class AdFrontendBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
$extension = $container->getExtension('security');
$extension->addSecurityListenerFactory(new ApiFactory());
}
}
答案 0 :(得分:1)
好问题。我知道我的回答有点迟,但这就是你的做法。
在您的工厂中,您具有此功能
public function getKey()
{
return 'user';
}
这是配置的关键。在Symfony \ Bundle \ SecurityBundle \ DependencyInjection \ Security \ Factory \ FormLoginFactory(启用UsernamePasswordFormAuthenticationListener的类)中,您具有以下功能:
public function getKey()
{
return 'form-login';
}
所以如果你看看你的security.yml
firewalls:
secured_area:
pattern: ^/
user: true
form_login: # <--- This is where you enables the default factory
login_path: /login
check_path: /check_login
logout: ~
您不想指定“form_login”。这是您启用UsernamePasswordFormAuthenticationListener的位置。一个好的默认配置是:
firewalls:
secured_area:
pattern: ^/
user: true
logout: ~
根据Fabien的blog post
,在Symfony2.4中配置应该更简单答案 1 :(得分:0)
您可以使用stopPropagation功能 http://api.symfony.com/2.0/Symfony/Component/EventDispatcher/Event.html#method_stopPropagation
$event->stopPropagation()
可在此处找到更多信息: http://symfony.com/doc/2.0/components/event_dispatcher/introduction.html#stopping-event-flow-propagation