【问题标题】:JWT Token not found 401 error when trying to access my api plateform?尝试访问我的 api 平台时未找到 JWT 令牌 401 错误?
【发布时间】:2019-12-05 15:16:26
【问题描述】:

我遇到了一个我真的不知道如何解决的小问题,我尝试了一些稍后会提到的解决方案,但仍然没有任何结果,这是一个身份验证问题,当我尝试让我的用户进行身份验证时。

我有一个像这样的用户实体:

<?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Security\Core\User\UserInterface;

/**
 * @ApiResource(normalizationContext={"groups"={"read"}})
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 */
class User implements UserInterface {
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups({"read"})
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"read"})
     */
    private $username;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"read"})
     */
    private $email;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $password;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Post", mappedBy="user")
     * @Groups({"read"})
     */
    private $posts;

    public function __construct(){
        $this->posts = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getUsername(): ?string
    {
        return $this->name;
    }

    public function setUsername(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getEmail(): ?string
    {
        return $this->email;
    }

    public function setEmail(string $email): self
    {
        $this->email = $email;

        return $this;
    }

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

    public function setPassword(string $password): self
    {
        $this->password = $password;

        return $this;
    }

    public function getRoles(){

    }

    public function getSalt(){

    }


    public function eraseCredentials(){

    }

    public function getPosts(): Collection
    {
        return $this->posts;
    }
}

这是我的 security.yaml 文件:

security:
    # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
    encoders:
        App\Entity\User:
            algorithm: bcrypt
    providers:
        #in_memory: { memory: ~ }
        database: 
            entity: 
                class: App\Entity\User
                property: username
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        api:
            pattern: ^/api
            stateless: true
            anonymous: true
            json_login: 
                check_path: /api/login_check
                success_handler: lexik_jwt_authentication.handler.authentication_success
                failure_handler: lexik_jwt_authentication.handler.authentication_failure
            guard: 
                authenticators: 
                    - lexik_jwt_authentication.jwt_token_authenticator

            # activate different ways to authenticate
            # https://symfony.com/doc/current/security.html#firewalls-authentication

            # https://symfony.com/doc/current/security/impersonating_user.html
            # switch_user: true

    # Easy way to control access for large sections of your site
    # Note: Only the *first* access control that matches will be used
    access_control:
        - { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/api, roles: IS_AUTHENTICATED_FULLY }
        # - { path: ^/admin, roles: ROLE_ADMIN }
        # - { path: ^/profile, roles: ROLE_USER }

所以我完全按照课程中提到的那样做,但仍然得到这个 401 错误,所以我尝试对一些文件进行一些更改,这是由面临相同问题的其他人提出的,我尝试在我的 . htaccess 文件:

 # Sets the HTTP_AUTHORIZATION header removed by Apache
RewriteEngine On
RewriteCond %{HTTP:Authorization} .
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

但仍然遇到同样的错误,所以我愿意尝试提到here 的其他解决方案,但我使用的是 Windows 而不是 linux,所以答案不符合我的需求。

我被困了将近一天,我需要帮助的人,任何帮助将不胜感激。

【问题讨论】:

    标签: php api jwt symfony4 api-platform.com


    【解决方案1】:

    我遇到了类似的问题并像这样修复它:

    security:
      firewalls:
        api:
          pattern: ^/login_check
          json_login: 
            check_path: /login_check
    
      access_control:
        - { path: ^/api/login_check, roles: IS_AUTHENTICATED_ANONYMOUSLY 
    

    这在 LexikJWTAuthenticationBundle GitHub 问题板上有所讨论:here

    【讨论】:

      【解决方案2】:

      如果您的问题在生产中,可能是由于 Apache Autorisation,请在此处查看答案:https://stackoverflow.com/a/70787257/3556984

      【讨论】:

        猜你喜欢
        • 2018-07-17
        • 2012-10-27
        • 2019-12-13
        • 1970-01-01
        • 2023-03-11
        • 2019-08-24
        • 2018-01-15
        • 1970-01-01
        • 2021-03-30
        相关资源
        最近更新 更多