這個問題是這個問題的下一部分:教義 getRepository 錯誤
在 Dylan 的幾次修復和幫助之后,我設法從這段代碼中得到了
public function getUser($credentials, UserProviderInterface $userProvider)
{
try {
$credentials = str_replace('Bearer ', '', $credentials);
$jwt = (array) JWT::decode(
$credentials,
$this->params->get('jwt_secret'),
['HS256']
);
dump($jwt['user']);
dump($this->em
->getRepository(User::class)//TODO Fix here
->findOneBy(['email' => $jwt['email']]));
return $this->em
->getRepository(User::class)//TODO Fix here
->findOneBy(['username' => $jwt['user']]);
}catch (\Exception $exception) {
throw new AuthenticationException($exception->getMessage());
}
}
在傾銷 jwt['user'] 和整個回報后,我得到了這個:
從郵遞員回傳
如您所見,我的兩個轉儲均有效,但仍然出現錯誤 xd。任何線索/想法為什么?
編輯 1:security.yaml:
security:
encoders:
App\Entity\User: bcrypt
enable_authenticator_manager: true
password_hashers:
Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
providers:
app_user_provider:
entity:
class: App\Entity\User
property: email
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
pattern: ^/api
stateless: true
provider: app_user_provider
guard:
authenticators:
- App\Security\JwtAuthenticator
Edit2 JwtAuthenticator.php :
namespace App\Security;
use App\Entity\User;
use App\Repository\UserRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
use Firebase\JWT\JWT;
use Symfony\Component\HttpFoundation\JsonResponse;
class JwtAuthenticator extends AbstractGuardAuthenticator
{
private $em;
private $params;
public function __construct(EntityManagerInterface $em, ContainerBagInterface $params)
{
$this->em = $em;
$this->params = $params;
}
public function supports(Request $request)
{
return $request->headers->has('Authorization');
}
public function getCredentials(Request $request)
{
return $request->headers->get('Authorization');
}
public function getUser($credentials, UserProviderInterface $userProvider)
{
try {
$credentials = str_replace('Bearer ', '', $credentials);
$jwt = (array) JWT::decode(
$credentials,
$this->params->get('jwt_secret'),
['HS256']
);
dump($jwt['user']);
dump($this->em
->getRepository(User::class)
->findOneBy(['email' => $jwt['email']]));
return $this->em
->getRepository(User::class)
->findOneBy(['username' => $jwt['user']]);
}catch (\Exception $exception) {
throw new AuthenticationException($exception->getMessage());
}
}
public function checkCredentials($credentials, UserInterface $user)
{
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
return new JsonResponse([
'message' => $exception->getMessage()
], Response::HTTP_UNAUTHORIZED);
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $providerKey)
{
return;
}
public function supportsRememberMe()
{
return false;
}
public function start(Request $request, AuthenticationException $authException = null)
{
$data = [
'message' => 'Authentication Required'
];
return new JsonResponse($data, Response::HTTP_UNAUTHORIZED);
}
}
uj5u.com熱心網友回復:
這個錯誤似乎很容易理解。
CheckCredentials 應該回傳 true 以使連接有效。
您可能需要檢查是否$user找到。但我相信只寫return true會起作用,因為如果找不到用戶,之前會拋出例外。
例如:
public function checkCredentials($credentials, UserInterface $user)
{
if ($user instanceof User){
return true;
}
return false;
}
這里做了類似的事情。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/422404.html
標籤:
