Symfony implement-自定义表单密码验证器http://symfony.com/doc/current/cookbook/security/custom_password_authenticator.html
因此,这部分代码通过UserProviderInterface
在services.yml中定义我的用户提供程序类,并返回UserInterface
。
public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
{
try {
$user = $userProvider->loadUserByUsername($token->getUsername());
默认情况下,我的自定义用户类提供程序使用更多函数实现AdvancedUserInterface
:http://api.symfony.com/2.6/Symfony/Component/Security/Core/User/AdvancedUserInterface.html
class Users implements AdvancedUserInterface, 'Serializable
问题:如何用UserProviderInterface
实现AdvancedUserInterface
。或者我只需要手动重新创建这个AdvancedUserInterface
函数,并在加载用户对象后手动检查?
谢谢。我希望你能理解。
编辑:它看起来像是AdvancedUser Interface函数没有在自定义密码身份验证中自动调用。所以在得到用户对象后需要调用手动这个函数
try {
$user = $userProvider->loadUserByUsername($token->getUsername());
if ($user->isAccountNonExpired()){ throw new AuthenticationException('expired');}
if ($user->isAccountNonLocked()){ throw new AuthenticationException('locked');}
if ($user->isCredentialsNonExpired()){ throw new AuthenticationException('credExpired');}
if (!$user->isEnabled()){ throw new AuthenticationException('disabled');}
您可能有一些困惑。
AdvancedUserInterface
-表示用户实例,并从UserInterface
扩展而来。
UserProviderInterface
-表示加载用户的提供程序(UserInterface
(。
在方法loadUserByUsername
中,您可以返回AdvancedUserInterface
实例。
例如:
用户:
class User implements AdvancedUserInterface
{
// .. Some fields and functions
}
用户提供商:
class MyUserProvider implements UserProviderInterface
{
public function loadUserByUsername($username)
{
// Get the User instance from DB or another system
return $user;
}
// Some functions
}