Symfony 4:从DB ind自定义用户提供程序中获取用户返回“Bad Credentials”

时间:2018-05-26 10:12:52

标签: api symfony jwt symfony4 lexikjwtauthbundle

我正在构建一个基于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"}

有谁知道这个问题?

0 个答案:

没有答案