Symfony-使用LexikJWTAuthenticationBundle设置TokenController
我正在使用LexikJWTAuthenticationBundle 我正在设置控制器以获取令牌:Symfony-使用LexikJWTAuthenticationBundle设置TokenController,symfony,symfony4,lexikjwtauthbundle,Symfony,Symfony4,Lexikjwtauthbundle,我正在使用LexikJWTAuthenticationBundle 我正在设置控制器以获取令牌: class TokenController extends AbstractController { /** * @Route("/api/token", name="token", methods={"POST"}) * @param Request $request * @param JWTEncoderInterface $JWTEncoder
class TokenController extends AbstractController
{
/**
* @Route("/api/token", name="token", methods={"POST"})
* @param Request $request
* @param JWTEncoderInterface $JWTEncoder
* @return JsonResponse
* @throws \Lexik\Bundle\JWTAuthenticationBundle\Exception\JWTEncodeFailureException
*/
public function token(Request $request, JWTEncoderInterface $JWTEncoder)
{
$user = $this->getDoctrine()->getRepository(User::class)->findOneBy([
'email' => $request->getUser(),
]);
if (!$user) {
throw $this->createNotFoundException('User Not Found');
}
$isValid = $this->get('security.password_encoder')
->isPasswordValid($user, $request->getPassword());
if (!$isValid) {
throw new BadCredentialsException();
}
$token = $JWTEncoder->encode([
'email' => $user->getEmail(),
'exp' => time() + 3600 // 1 hour expiration
]);
return new JsonResponse(['token' => $token]);
}
}
但我有一个错误:
未找到服务“security.password\u encoder”:即使它存在
在应用程序的容器中
“App\Controller\TokenController”是一个较小的服务定位器,用于
只知道“条令”、“form.factory”、“http_内核”,
“参数包”、“请求堆栈”、“路由器”,
“security.authorization\u checker”、“security.csrf.token\u manager”,
“安全性。令牌存储”、“序列化程序”、“会话”和“细枝”服务。
除非您需要额外的惰性,否则请尝试使用依赖项注入
相反否则,您需要使用
“TokenController::getSubscribedServices()
我已经使用了依赖注入,这是我的服务配置
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
public: false # Allows optimizing the container by removing unused services; this also means
# fetching services directly from the container via $container->get() won't work.
# The best practice is to be explicit about your dependencies anyway.
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
# controllers are imported separately to make sure services can be injected
# as action arguments even if you don't extend any base controller class
App\Controller\:
resource: '../src/Controller'
tags: ['controller.service_arguments']
问题在哪里 您正在从
AbstractController
扩展,使用此控制器,您使用$this->get()访问的服务将受到限制。要访问密码编码器服务,您可以将Symfony\Component\Security\Core\encoder\UserPasswordEncoderInterface
注入控制器操作或通过控制器类构造函数
private $passwordEncoder;
public function __construct(UserPasswordEncoderInterface $passwordEncoder)
{
$this->passwordEncoder = $passwordEncoder;
}
...
$this->passwordEncoder->isPasswordValid()
...
或者您可以从Symfony\Bundle\FrameworkBundle\Controller\Controller
进行扩展,以实现完整的容器访问<代码>$this->get('security.password\u encoder')
应该可以使用它