Silex应用程序中的自定义身份验证提供程序
我尝试使用silex文档编写用于LDAP身份验证的自定义身份验证提供程序-
I try to write custom authentication provider for LDAP-authentication using silex documentation - Defining a custom Authentication Provider.
但是,如果我查看$app['security.authentication_providers']
,则有两个提供程序.我定义的一个App\LdapAuthenticationProvider
和一个Symfony\Component\Security\Core\Authentication\Provider\DaoAuthenticationProvider
But if I look into $app['security.authentication_providers']
there are two providers. One that I defined App\LdapAuthenticationProvider
and one Symfony\Component\Security\Core\Authentication\Provider\DaoAuthenticationProvider
当我尝试授权用户时,我得到了错误,因为类DaoAuthenticationProvider调用了App\LdapUserProvider::loadUserByUsername()
.
And when I try to authorize user I get error because there is call of a App\LdapUserProvider::loadUserByUsername()
from class DaoAuthenticationProvider.
如果在$app['security.authentication_providers']
中只有一个提供程序,我想我应该不会出错,因为我的LDAP提供程序不会调用loadUserByUsername.
If I would have only one provider in $app['security.authentication_providers']
I think I should not get error because my LDAP-provider do not call loadUserByUsername.
这是$app['security.authentication_providers']
的转储
array (size=2)
0 => object(App\LdapAuthenticationProvider)[194]
private 'userProvider' =>
object(App\LdapUserProvider)[176]
private 'ldap' => resource(57, ldap link)
private 'defaultRoles' =>
array (size=1)
...
private 'providerKey' => string 'default' (length=7)
1 => object(Symfony\Component\Security\Core\Authentication\Provider\DaoAuthenticationProvider)[195]
private 'encoderFactory' =>
object(Symfony\Component\Security\Core\Encoder\EncoderFactory)[197]
private 'encoders' =>
array (size=1)
...
private 'userProvider' =>
object(App\LdapUserProvider)[176]
private 'ldap' => resource(57, ldap link)
private 'defaultRoles' =>
array (size=1)
...
private 'hideUserNotFoundExceptions' (Symfony\Component\Security\Core\Authentication\Provider\UserAuthenticationProvider) => boolean true
private 'userChecker' (Symfony\Component\Security\Core\Authentication\Provider\UserAuthenticationProvider) => object(Symfony\Component\Security\Core\User\UserChecker)[196]
private 'providerKey' (Symfony\Component\Security\Core\Authentication\Provider\UserAuthenticationProvider) => string 'default' (length=7)
那么,有人知道为什么还有额外的提供者,我该如何摆脱呢?
So, does anybody know why there are extra provider and how can I get rid of it?
其中有引导应用程序, LdapAuthenticationListener 的代码>和 LdapAuthenticationProvider .
问题已解决.
我刚刚用symfony2 UsernamePasswordFormAuthenticationListener
扩展了LdapAuthenticationListener
类,并像这样更改bootstarp:
I've just extended my LdapAuthenticationListener
class with symfony2 UsernamePasswordFormAuthenticationListener
and change bootstarp like this:
$app['security.authentication_listener.factory.ldap'] = $app->protect(
function ($name, $options) use ($app) {
$app['security.authentication_provider.'.$name.'.ldap'] = $app->share(
function () use ($app) {
return new LdapAuthenticationProvider(
$app['security.user_provider.default'],
'ldap'
);
}
);
$app['security.authentication_listener.'.$name.'.ldap'] = $app->share(
function () use ($app, $name, $options) {
$app['security.authentication.success_handler.'.$name] =
$app['security.authentication.success_handler._proto']($name, $options);
$app['security.authentication.failure_handler.'.$name] =
$app['security.authentication.failure_handler._proto']($name, $options);
return new LdapAuthenticationListener(
$app['security'],
$app['security.authentication_manager'],
$app['security.session_strategy'],
$app['security.http_utils'],
$name,
$app['security.authentication.success_handler.'.$name],
$app['security.authentication.failure_handler.'.$name],
array_merge(
array(
'check_path' => '/admin/login_check',
'login_path' => '/login',
),
$options
),
$app['logger'],
$app['dispatcher'],
null
);
}
);
return array(
'security.authentication_provider.'.$name.'.ldap',
'security.authentication_listener.'.$name.'.ldap',
null,
'pre_auth'
);
}
我需要自定义身份验证侦听器以覆盖身份验证方法中的令牌,并且身份验证提供程序通过用户名和密码从用户提供程序中检索用户$this->userProvider->loadUserByUsernameAndPassword($usernam, $password)
I need custom authentication listener to overwrite token in authentication method and authentication provider retrieve user from user provider by username and password $this->userProvider->loadUserByUsernameAndPassword($usernam, $password)