【问题标题】:LexikJWTAuthenticationBundle with Doctrine --> JWT Token not foundLexikJWTAuthenticationBundle with Doctrine --> JWT Token not found
【发布时间】:2019-09-04 05:53:55
【问题描述】:

我正在尝试通过 API 平台将 LexikJWTAuthenticationBundle 与 Doctrine 用户管理一起使用。 配置后我总是收到 {"code":401,"message":"未找到 JWT 令牌"}

1) 我按照https://github.com/lexik/LexikJWTAuthenticationBundle/blob/master/Resources/doc/index.md 中的描述安装了 LexikJWTAuthenticationBundle,我将 routes.yaml 更改为路径 /login_check 而不是 /api/login_check

2)我生成了Entity User,并使用了doctrine来生成数据库表。另外我创建了类 UserRepository

3) 我将 security.yaml 更改为

# app/config/packages/security.yaml
security:
    encoders:
        FOS\UserBundle\Model\UserInterface: bcrypt

    role_hierarchy:
        ROLE_READER: ROLE_USER
        ROLE_ADMIN: ROLE_READER

    providers:
        users:
            entity:
                # the class of the entity that represents users
                class: 'App\Entity\User'
                # the property to query by - e.g. username, email, etc
                property: 'username'
                # optional: if you're using multiple Doctrine entity
                # managers, this option defines which one to use
                # manager_name: 'customer'

    firewalls:
        login:
            pattern:  ^/login
            stateless: true
            anonymous: true
            provider: users
            json_login:
                check_path: /login_check
                username_path: email
                password_path: password
                success_handler: lexik_jwt_authentication.handler.authentication_success
                failure_handler: lexik_jwt_authentication.handler.authentication_failure

        main:
            pattern:   ^/
            provider: users
            stateless: true
            anonymous: true
            guard:
                authenticators:
                - lexik_jwt_authentication.jwt_token_authenticator

        dev:
            pattern:  ^/(_(profiler|wdt)|css|images|js)/
            security: false

    access_control:
    - { path: ^/login, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/books, roles: [ ROLE_READER ] }
    - { path: ^/, roles: [ ROLE_READER ] }

此外,我将 api_platform.yaml 更改为

parameters:
    # Adds a fallback VARNISH_URL if the env var is not set.
    # This allows you to run cache:warmup even if your
    # environment variables are not available yet.
    # You should not need to change this value.
    env(VARNISH_URL): ''

api_platform:
    swagger:
        api_keys:
            apiKey:
                name: Authorization
                type: header
    mapping:
        paths: ['%kernel.project_dir%/src/Entity']
    title: Hello API Platform
    version: 1.0.0
    #Varnish integration, remove if unwanted
#    http_cache:
#        invalidation:
#            enabled: true
#            varnish_urls: ['%env(VARNISH_URL)%']
#        max_age: 0
#        shared_max_age: 3600
#        vary: ['Content-Type', 'Authorization']
#        public: true
    # Mercure integration, remove if unwanted
    mercure:
        hub_url: '%env(MERCURE_SUBSCRIBE_URL)%'

用户看起来像

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;

/**
 * @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=60, unique=true)
     */
    private $email;

    /**
     * @ORM\Column(name="is_active", type="boolean")
     */
    private $isActive;

    public function __construct() // add $username
    {
        $this->isActive = 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_ADMIN');
    }

    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);
    }
}

如果我使用 curl,我会收到以下错误: 找不到路径“/login_check”的控制器。路由配置错误。 (404 未找到)

我的错误在哪里?提前致谢。

【问题讨论】:

  • 你在 routes.yml 中添加了什么?您是否收到 401 或 404 错误消息?如果你两个都得到,我觉得很奇怪,你能详细说明你是如何得到这些的吗?你检查过这个Question,可能是重复的吗?
  • 路由的内容,yml 是login_check: path: /login_check methods: [POST] 我已经阅读了链接的帖子,但没有找到解决我的问题的方法。当我将 Content-Type: application/json 添加到请求中时,我收到响应 "code": 401, "message": "Bad credentials" 我存储的密码错误并且招摇我的身份验证有问题跨度>
  • 所以我认为您设置了新密码并再次尝试?同样的错误?也许您可以在您的User 实体类中分享来自UserInterface 的实现方法?
  • 我刚刚在上面的问题中添加了 User 实体类。

标签: api-platform.com


【解决方案1】:

如果您使用 Apache 并且您有 {"code": 401, "message": "JWT token not found"},则问题可能是您在虚拟主机中的重写规则。 就我而言,我从我的虚拟主机中删除了重写规则,并将我丢失的 .htaccess 添加到 /public 文件夹(Symfony 4)中

PS:/web Symfony 文件夹


Lexik JWT Token not found

【讨论】:

    猜你喜欢
    • 2017-07-16
    • 1970-01-01
    • 2019-05-12
    • 2019-11-02
    • 2016-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-02
    相关资源
    最近更新 更多