0

我有一个 Symfony4 安装,其中安装了一些常用的 Flex 捆绑包

  • 制作
  • jwt-身份验证
  • 注解
  • 行为
  • phpunit
  • 服务器

我有这个路由文件:

api_login_check:
    path: /api/login_check

我有这个配置:

security:

    encoders:
        App\Security\User: plaintext

    providers:
        app.provider:
            id: App\Security\UserProvider

    firewalls:

        login:
            pattern:  ^/api/login
            stateless: true
            anonymous: true
            provider: app.provider
            form_login:
                check_path: /api/login_check
                success_handler: lexik_jwt_authentication.handler.authentication_success
                failure_handler: lexik_jwt_authentication.handler.authentication_failure
                require_previous_session: false

        api:
            pattern:   ^/api
            stateless: true
            provider: app.provider
            guard:
                authenticators:
                    - lexik_jwt_authentication.jwt_token_authenticator

    access_control:
        - { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/api,       roles: IS_AUTHENTICATED_FULLY }

这个用户:

<?php

namespace App\Security;

use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\EquatableInterface;

class User implements UserInterface, EquatableInterface
{
    private $username;
    private $password;
    private $salt;
    private $roles;

    public function __construct($username, $password, $salt, array $roles)
    {
        $this->username = $username;
        $this->password = $password;
        $this->salt = $salt;
        $this->roles = $roles;
    }

    public function getRoles()
    {
        return $this->roles;
    }

    public function getPassword()
    {
        return $this->password;
    }

    public function getSalt()
    {
        return $this->salt;
    }

    public function getUsername()
    {
        return $this->username;
    }

    public function eraseCredentials()
    {
    }

    public function isEqualTo(UserInterface $user)
    {
        if (!$user instanceof User) {
            return false;
        }

        if ($this->password !== $user->getPassword()) {
            return false;
        }

        if ($this->salt !== $user->getSalt()) {
            return false;
        }

        if ($this->username !== $user->getUsername()) {
            return false;
        }

        return true;
    }
}

最后是这个用户提供者:

<?php

namespace App\Security;

use 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 UserProvider implements UserProviderInterface
{
    public function loadUserByUsername($username)
    {
        $username = 'senso';
        $password = 'rario';
        $salt     = 'sale';
        $roles    = ['ROLE_ADMIN'];

        return new User($username, $password, $salt, $roles);

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

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

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

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

当我跑

curl -X POST http://localhost:8000/api/login_check -d _username=senso -d _password=rario {"code":401,"message":"Bad credentials"}

我总是得到

{"code":401,"message":"凭证错误"}

我的问题是:

  • 如何解决这个问题?
  • 为什么从未调用过 UserProvider::loadUserByUsername() ?
4

1 回答 1

1

如果您正在使用并向您的班级PlaintextPasswordEncoder提供 a ,那么您的方法应该返回。saltUserUsergetPasswordplain_password{salt}

在这种情况下,

$username = 'senso';
$password = 'rario{sale}';
$salt     = 'sale';
$roles    = ['ROLE_ADMIN'];

return new User($username, $password, $salt, $roles);

或者

$username = 'senso';
$password = 'rario';
$salt     = '';
$roles    = ['ROLE_ADMIN'];

return new User($username, $password, $salt, $roles);

会很好用。

于 2018-04-01T00:27:06.617 回答