我测试了in_memory身份验证,并使用他们的默认UserProvider进行数据库身份验证,它工作正常,但是当我指定costum userProvider时,它会无声地失败,我使用表单登录,它只刷新页面,而我我还没有通过身份验证。
Costum Userprovider:
namespace Snap\ModelBundle\Security;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Snap\RestBundle\Entity\User;
class WebserviceUserProvider implements UserProviderInterface {
private $em;
public function __construct($em) {
$this->em = $em;
}
public function loadUserByUsername($username) {
$userData = $this->em
->getRepository('SnapRestBundle:User')
->findOneBy(array('username' => $username));
// make a call to your webservice here
// pretend it returns an array on success, false if there is no user
if ($userData) {
return $userData;
}
throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
}
public function refreshUser(UserInterface $user) {
if (!$user instanceof User) {
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
}
return $this->loadUserByUsername($user->getUsername());
}
public function supportsClass($class) {
return $class === 'Snap\ModelBundle\Entity\User';
}
}
Security.yml
security:
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
pattern: ^/
anonymous: ~
provider: users
form_login:
login_path: snap_front_login
check_path: login_check
encoders:
Snap\RestBundle\entity\User: plaintext
providers:
users:
id: webserviceUserProvider
role_hierarchy:
ROLE_USER: ROLE_USER
ROLE_ADMIN: [ROLE_USER, ROLE_ADMIN]
ROLE_SUPER_ADMIN: [ROLE_SUPER_ADMIN, ROLE_ALLOWED_TO_SWITCH ]
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/, roles: ROLE_USER }
答案 0 :(得分:0)
以下代码似乎没有返回实现UserInterface
接口的类:
if ($userData) {
return $userData;
}
您需要返回实现UserInterface
接口的类的实例。
来自Symfony官方文档,“How to create a custom User Provider”:
(...)您需要创建一个表示该数据的User类。用户 可以看你想要的并包含任何数据。唯一的要求 是该类实现UserInterface。
示例强>
例如,这是InMemoryUserProvider
创建用户实例的方式:
return new User($user->getUsername(), $user->getPassword(), $user->getRoles(), $user->isEnabled(), $user->isAccountNonExpired(),
$user->isCredentialsNonExpired(), $user->isAccountNonLocked());