我在通過 jwt 進行身份驗證時遇到問題: public function getUser($credentials, UserProviderInterface $userProvider)
{
try {
$credentials = str_replace('Bearer ', '', $credentials);
$jwt = (array) JWT::decode(
$credentials,
$this->params->get('jwt_secret'),
['HS256']
);
return $this->em->getRepository(User::class)
->findOneBy([
'username' => $jwt['user'],
]);
}catch (\Exception $exception) {
throw new AuthenticationException($exception->getMessage());
}
}
我有這個功能,在輸入有效憑據時,我收到此錯誤:“憑據檢查失敗,因為傳遞給 CustomCredentials 的可呼叫物件沒有回傳 \u0022true\u0022。”
我想也許回傳函式 getRepository 想要我的用戶物體存盤庫,我傳遞了引數:getRepository(UserRepository::class) 但我得到了不同的錯誤:在鏈配置的命名空間 App\Entity 中找不到類 \u0027App\UserRepository\u0027
現在我有點卡住了,沒有任何好主意,我會很感激和 ides/thoguhts/clues
編輯1。我認為可能在 getRepository() 引數中應該作為字串傳遞,但也沒有,我得到錯誤:類 \u0027User\u0027 不存在
編輯2。用戶存盤庫.php:
<?php
namespace App\Repository;
use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
/**
* @method User|null find($id, $lockMode = null, $lockVersion = null)
* @method User|null findOneBy(array $criteria, array $orderBy = null)
* @method User[] findAll()
* @method User[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class UserRepository extends ServiceEntityRepository implements PasswordUpgraderInterface
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, User::class);
}
public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $newHashedPassword): void
{
if (!$user instanceof User) {
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', \get_class($user)));
}
$user->setPassword($newHashedPassword);
$this->_em->persist($user);
$this->_em->flush();
}
// /**
// * @return User[] Returns an array of User objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('u')
->andWhere('u.exampleField = :val')
->setParameter('val', $value)
->orderBy('u.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?User
{
return $this->createQueryBuilder('u')
->andWhere('u.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}
和 User.php 物體:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\UserInterface;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ApiResource()
* @ORM\Entity
* @UniqueEntity(fields="email", message="Email already taken")
* @UniqueEntity(fields="username", message="Username already taken")
*/
#[ApiResource]
class User implements UserInterface
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @ORM\Column(type="string", length=255, unique=true)
* @Assert\NotBlank()
* @Assert\Email()
*/
private $email;
/**
* @ORM\Column(type="string", length=255, unique=true)
* @Assert\NotBlank()
*/
private $username;
/**
* @Assert\NotBlank()
* @Assert\Length(max=4096)
*/
private $plainPassword;
/**
* @ORM\Column(type="string", length=64)
*/
private $password;
/**
* @ORM\Column(type="array")
*/
private $roles;
public function __construct(){
$this->roles = array('ROLE_USER');
}
public function getEmail(){
return $this->email;
}
public function setEmail($email){
$this->email = $email;
}
public function getUsername(){
return $this->username;
}
public function setUsername($username){
$this->username = $username;
}
public function getPlainPassword(){
return $this->plainPassword;
}
public function setPlainPassword($password){
$this->plainPassword = $password;
}
public function getPassword(){
return $this->password;
}
public function setPassword($password){
$this->password = $password;
}
public function getSalt(){
return null;
}
public function getRoles(){
return $this->roles;
}
public function eraseCredentials(){
}
}
uj5u.com熱心網友回復:
您缺少指向您的存盤庫類的鏈接。
編輯您的@ORM\Entity注釋:
use App\Repository\UserRepository;
/**
* @ApiResource()
* @ORM\Entity(repositoryClass=UserRepository::class)
* @UniqueEntity(fields="email", message="Email already taken")
* @UniqueEntity(fields="username", message="Username already taken")
*/
#[ApiResource]
class User implements UserInterface
{
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/422405.html
標籤:
