我开发自己的用户提供商 . 为了检索用户,我已经注入了我的UserRepository:
app_bundle.api_key_user_provider:
class: AppBundle\Security\User\ApiKeyUserProvider
factory: ["@doctrine.orm.entity_manager", "getRepository"]
arguments:
- AppBundle\Entity\User
我还将EntityManager添加到我的提供者的 __construct()
方法中:
class ApiKeyUserProvider implements UserProviderInterface
{
public function __construct(EntityManager $em)
{
$this->em = $em;
}
public function loadUserByUsername($email)
{
$user = $this->em->findBy(array('email' => $email));
if (!$user) {
throw new UsernameNotFoundException('User not found!');
}
$this->user = $user;
return $user;
}
public function refreshUser(UserInterface $user)
{
return $user;
}
public function supportsClass($class)
{
return $class === 'AppBundle\Entity\User';
}
}
但是当调用Provider时,我得到了下一个错误:
可捕获的致命错误:参数1传递给Symfony \ Component \ Security \ Core \ Authentication \ Provider \ DaoAuthenticationProvider :: __ construct()必须实现接口Symfony \ Component \ Security \ Core \ User \ UserProviderInterface,给出AppBundle \ Repository \ UserRepository的实例
我试图注入 Symfony\Component\Security\Core\User\UserProviderInterface
,但没有成功 . 我作为参数传递给 services.yml
提供者定义下一个字符串:
-
Symfony \ Component \ Security \ Core \ User \ UserProviderInterface(得到错误"class not exists")
-
Vendor \ Symfony \ Component \ Security \ Core \ User \ UserProviderInterface(得到相同的错误)
我也在SecurityBundle中搜索了所需的服务,但一无所获 .
My question :如何将 Symfony\Component\Security\Core\User\UserProviderInterface
注入我的 ApiKeyUserProvider
或我还能做些什么来解决这个问题?
非常感谢您的帮助!
1 回答
您的
ApiKeyUserProvider
期望EntityManager作为依赖项 . 在这种情况下,您的服务定义是错误的 - 只需将其放入services.yml即可这也需要在您的课程中进行一些调整,因为
EntityManager
没有findBy
方法,因为您正在尝试使用它 . 所以你需要改变成
EDIT: 根据您的评论我假设您要注入
UserRepository
,而不是整个EntityManager
. 为此,您应将UserRepository
定义为服务并将其注入您的 class .services.yml
ApiKeyUserProvider