我正在尝试按照文档中编写的说明在 Api 平台项目中实现 JWT 身份验证。如果我使用内存提供程序,它可以工作,但是当我尝试使用实体提供程序配置它时,我总是得到响应 401 bad credentials。
如果我更改提供程序并使用“in_memory”而不是“my_own_provider”,它会起作用。我有一些用户的固定装置,我检查了数据库,它正确地包含了用户的行。
这是我的 security.yml
security:
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
# encoders:
# App\Entity\User: bcrypt
encoders:
App\Entity\User: plaintext
Symfony\Component\Security\Core\User\User: plaintext
providers:
in_memory:
memory:
users:
user:
password: user-test
roles: 'ROLE_USER'
admin:
password: admin-test
roles: 'ROLE_ADMIN'
my_own_provider:
entity:
class: App\Entity\User
property: username
# if you're using multiple entity managers
# manager_name: customer
firewalls:
login:
pattern: ^/api/login
stateless: true
anonymous: true
provider: my_own_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_documentation:
pattern: ^/api/documentation
anonymous: ~
provider: my_own_provider
api:
pattern: ^/
stateless: true
provider: my_own_provider
guard:
authenticators:
- lexik_jwt_authentication.jwt_token_authenticator
access_control:
- { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api/documentation, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/, roles: IS_AUTHENTICATED_FULLY }
role_hierarchy:
ROLE_API: [ROLE_USER]
这是我的实体类:
<?php
// src/Entity/User.php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Table(name="app_users")
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
*/
class User implements UserInterface, \Serializable
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=25, unique=true)
*/
private $username;
/**
* @ORM\Column(type="string", length=64)
*/
private $password;
/**
* @ORM\Column(type="string", length=254, unique=true)
*/
private $email;
/**
* @ORM\Column(name="is_active", type="boolean")
*/
private $isActive;
public function __construct()
{
$this->isActive = true;
// may not be needed, see section on salt below
// $this->salt = md5(uniqid('', true));
}
public function getUsername()
{
return $this->username;
}
public function getSalt()
{
// you *may* need a real salt depending on your encoder
// see section on salt below
return null;
}
public function getPassword()
{
return $this->password;
}
public function getRoles()
{
return array('ROLE_USER');
}
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @param mixed $id
*/
public function setId( $id )
{
$this->id = $id;
}
/**
* @return mixed
*/
public function getEmail()
{
return $this->email;
}
/**
* @param mixed $email
*/
public function setEmail( $email )
{
$this->email = $email;
}
/**
* @return mixed
*/
public function getIsActive()
{
return $this->isActive;
}
/**
* @param mixed $isActive
*/
public function setIsActive( $isActive )
{
$this->isActive = $isActive;
}
public function eraseCredentials()
{
}
/** @see \Serializable::serialize() */
public function serialize()
{
return serialize(array(
$this->id,
$this->username,
$this->password,
// see section on salt below
// $this->salt,
));
}
/** @see \Serializable::unserialize() */
public function unserialize($serialized)
{
list (
$this->id,
$this->username,
$this->password,
// see section on salt below
// $this->salt
) = unserialize($serialized);
}
}
我发现有些人问了同样的问题,但对我没有用。
1 示例这与我的问题非常相似,但我认为服务器配置不是我的问题,因为使用 in_memory 它可以工作。我也尝试了最后一个解决方案 json_login 而不是 form_login 并且我有一个错误。
2 Example Here is Unauthorized 但我的问题是我无法获得令牌。
我也尝试使用 bcrypt 而不是纯文本,但没有成功。
有什么建议么?