gpt4 book ai didi

symfony - 401 未找到 JWT token

转载 作者:行者123 更新时间:2023-12-05 01:30:37 24 4
gpt4 key购买 nike

我提供了两个版本的 security.yaml文件。第二版根据API Platform文档。 API Platform发送到 creation a custom user provider .对于第二个选项 security.yaml在 API 平台文档中推荐,我需要创建两个额外的文件。我没有将它们附加到主题中,但如有必要,我会这样做。

但我认为问题出在 JWT 中。

环境:

  • 节点 v8.9.4
  • Chrome 64.0.3282.119
  • Ubuntu 16.04
  • axios 版本:0.16.2
  • Vue.js 2.4.2
  • vue-axios 2.0.2
  • api 平台/api 包:1.0
  • Symfony 4.0.4

  • 用户.php
    <?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=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);
    }
    }

    第一个选项 security.yaml
    security:

    encoders:
    App\Entity\User:
    algorithm: bcrypt

    providers:

    our_db_provider:
    entity:
    class: App\Entity\User
    property: username

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

    login:
    pattern: ^/api/login
    stateless: true
    anonymous: true
    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: our_db_provider
    guard:
    authenticators:
    - lexik_jwt_authentication.jwt_token_authenticator

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

    第二个选项 security.yaml
    security:

    encoders:
    App\Entity\User:
    algorithm: bcrypt

    App\Security\User\WebserviceUser: bcrypt

    providers:

    our_db_provider:
    entity:
    class: App\Entity\User
    property: username

    webservice:
    id: App\Security\User\WebserviceUserProvider

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

    login:
    pattern: ^/api/login
    stateless: true
    anonymous: true
    provider: webservice
    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: our_db_provider
    guard:
    authenticators:
    - lexik_jwt_authentication.jwt_token_authenticator
    access_control:
    - { path: ^/admin, roles: ROLE_ADMIN }
    - { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/api, roles: IS_AUTHENTICATED_FULLY }

    标题

    headers

    curl

    curl

    带标题的 curl

    curl with headers

    在浏览器中

    in browser

    .env
    ###> lexik/jwt-authentication-bundle ###
    # Key paths should be relative to the project directory
    JWT_PRIVATE_KEY_PATH=var/jwt/private.pem
    JWT_PUBLIC_KEY_PATH=var/jwt/public.pem
    JWT_PASSPHRASE=d70414362252a41ce772dff4823d084d
    ###< lexik/jwt-authentication-bundle ###

    lexik_jwt_authentication.yaml
    lexik_jwt_authentication:
    private_key_path: '%kernel.project_dir%/%env(JWT_PRIVATE_KEY_PATH)%'
    public_key_path: '%kernel.project_dir%/%env(JWT_PUBLIC_KEY_PATH)%'
    pass_phrase: '%env(JWT_PASSPHRASE)%'

    最佳答案

    我的解决方案是在 .htaccess 中添加它

    RewriteCond %{HTTP:Authorization} ^(.*)
    RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]

    关于symfony - 401 未找到 JWT token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48667976/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com