【发布时间】:2018-09-10 12:48:25
【问题描述】:
我有一个 Symfony4 安装,其中安装了一些常用的 Flex 包
- 制作
- jwt-auth
- 注释
- 行为
- 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":"凭证错误"}
我总是得到
{"code":401,"message":"凭证错误"}
我的问题是:
- 如何解决这个问题?
- 为什么从未调用过 UserProvider::loadUserByUsername()?
【问题讨论】:
-
您可以发布您尝试过的正确
curl吗?我想检查一下:网址是否正确(而不是http://localhost:800ogin_check),选择的密码是rario,而不是test。 -
可以在
login防火墙下添加provider: app.provider吗? -
完成了,还是不行
-
我假设您在路由器配置中注册了
/api/login_check,您能确认一下吗? -
是的。我已经回答了问题
标签: php symfony jwt lexikjwtauthbundle