【问题标题】:Symfony 5 JWT Token not found找不到 Symfony 5 JWT 令牌
【发布时间】:2021-10-31 12:09:14
【问题描述】:

我正在使用带有 apache 服务器的 docker 映像,我正在尝试使用 Symfony 5Symfony 5 上运行 JWT 身份验证 LexikJWTAuthenticationBundle。我遵循了官方文档LexikJWTAuthenticationDocs 这是我所做的:

我运行了命令:php bin/console lexik:jwt:generate-keypair 这给了我一个 publicprivate 密钥对。我确保私钥未加密,正如我在此帖子答案中看到的那样:Encrypted private key

这里是security.yaml的配置:

security:
    encoders:
        App\Entity\User:
            algorithm: bcrypt

    providers:
    # used to reload user from session & other features (e.g. switch_user)
        database:
            entity:
                class: App\Entity\User
                property: email
    firewalls:
        login:
            pattern: ^/api/login
            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

        api:
            pattern: ^/api
            stateless: true
            guard:
                authenticators:
                    - lexik_jwt_authentication.jwt_token_authenticator

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

这是我的routes.yaml

api_login_check:
    path: /api/login_check

lexik_jwt_authentication.yaml

lexik_jwt_authentication:
    secret_key: '%env(resolve:JWT_SECRET_KEY)%'
    public_key: '%env(resolve:JWT_PUBLIC_KEY)%'
    pass_phrase: '%env(JWT_PASSPHRASE)%'
#    token time to live 1 hour
    token_ttl: 3600

.env

JWT_SECRET_KEY=%kernel.project_dir%/config/jwt/private.pem
JWT_PUBLIC_KEY=%kernel.project_dir%/config/jwt/public.pem
JWT_PASSPHRASE=test

通过所有这些配置,我使用邮递员从用户那里检索令牌:

postman get token

使用这个令牌,我现在正尝试从api/users 的每个用户的 api 获取

postman get users 但响应是 401 状态JWT Token not found

我的 apache 配置是:

<VirtualHost *:80>

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    RewriteEngine On
    RewriteCond %{HTTP:Authorization} ^(.*)
    RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]


    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

这可能与 apache 配置有关,正如我在很多 Stack Overflow 问题中看到的那样,但我已经尝试了很多,但即使是 LexikJWTAuthenticationBundle强>:

SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1

为什么我可以创建令牌但不能使用他访问数据?

【问题讨论】:

标签: apache symfony jwt


【解决方案1】:

这与.htaccess000-default.conf 无关,您必须更改conf in : /etc/apache2/apache2.conf 并在文件末尾添加SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1

但是我没有找到任何资源来解释为什么 apache 删除授权标头

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-28
    • 2020-05-23
    • 2016-05-17
    • 2018-07-17
    • 2020-05-19
    • 2016-06-16
    • 2016-02-17
    • 1970-01-01
    相关资源
    最近更新 更多