0

我正在构建一个基于 Symfony 4 的 API。

在我的自定义用户提供程序中,我从数据库中转储用户电子邮件和用户数据。显示电子邮件,但不显示第二个转储。在获取用户数据时,它会返回“Bad Credentials”。

这是我的用户提供者:

<?php
// src/Security/User/WebserviceUserProvider.php
namespace App\Security\User;

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;

class WebserviceUserProvider implements UserProviderInterface
{
    private $doctrine;

    public function __construct (\Doctrine\Bundle\DoctrineBundle\Registry $doctrine)
    {
        $this->doctrine = $doctrine;
    }

    public function loadUserByUsername($email)
    {
        var_dump($email);
        $userData = $this->doctrine->getManager()
            ->createQueryBuilder('SELECT u FROM users u WHERE u.email = :email')
            ->setParameter('email', $email)
            ->getQuery()
            ->getOneOrNullResult();
        var_dump($userData);exit;
        // pretend it returns an array on success, false if there is no user

        if ($userData) {
            $username = $userData['email'];
            $password = $userData['password'];
            $salt = $userData['salt'];
            $roles = $userData['roles'];

            // ...

            return new WebserviceUser($username, $password, $salt, $roles);
        }

        throw new UsernameNotFoundException(
            sprintf('Username "%s" does not exist.', $username)
        );
    }

    public function refreshUser(UserInterface $user)
    {
        if (!$user instanceof WebserviceUser) {
            throw new UnsupportedUserException(
                sprintf('Instances of "%s" are not supported.', get_class($user))
            );
        }

        return $this->loadUserByUsername($user->getUsername());
    }

    public function supportsClass($class)
    {
        return WebserviceUser::class === $class;
    }
}

如果我发送我的 json 登录数据,它会返回以下内容:

string(13) "test@test.com" {"code":401,"message":"Bad credentials"}

有谁知道这个问题?

4

0 回答 0