亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Symfony 4 - LexikJWTAuthenticationBundle 未找到 JWT

Symfony 4 - LexikJWTAuthenticationBundle 未找到 JWT

PHP
拉莫斯之舞 2023-09-22 16:53:10
下午好,我嘗試在我的項目中使用 LexikJWTAuthenticationBundle,但我遇到了未生成令牌的問題。我已在 var/jwt 目錄中設置私鑰和公鑰。當我嘗試使用登錄路由時,API 返回此響應:{    "code": 401,    "message": "JWT Token not found"}阿帕奇虛擬主機:<VirtualHost *:80>    ServerName ypostirixi    DocumentRoot "/var/www/ypostirixi/public"    RewriteEngine On    RewriteCond %{HTTP:Authorization} ^(.*)    RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]</VirtualHost>公共目錄中的.htaccess文件:<IfModule mod_rewrite.c>    RewriteEngine On    RewriteCond %{HTTP:Authorization} ^(.*)    RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]    # Send would-be 404 requests to Craft    RewriteCond %{REQUEST_FILENAME} !-f    RewriteCond %{REQUEST_FILENAME} !-d    RewriteRule (.+) index.php?p=$1 [QSA,L]</IfModule>security.yaml 安全性:encoders:    App\Entity\User:        algorithm: bcryptproviders:    doctrine_provider:        entity:            class: App\Entity\User            property: emailfirewalls:    dev:        pattern: ^/(_(profiler|wdt)|css|images|js)/        security: false    api_doc:        pattern:  ^/api/doc        security: false    api:        pattern:   ^/api        stateless: true        guard:            authenticators:                - lexik_jwt_authentication.jwt_token_authenticator    main:        pattern:   ^/        stateless: true        guard:            authenticators:                - lexik_jwt_authentication.jwt_token_authenticator        provider: doctrine_provideraccess_control:    - { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }    - { path: ^/api,       roles: IS_AUTHENTICATED_FULLY }我希望成功使用登錄路由并在其他路由上生成有效令牌。
查看完整描述

3 回答

?
慕森王

TA貢獻1777條經驗 獲得超3個贊

也許您忘記在防火墻部分或登錄參數(電子郵件作為用戶名)中配置登錄防火墻..

檢查這個


1配置/包/security.yaml


? ? firewalls:

? ? ? ? login:

? ? ? ? ? ? pattern:? ^/api/login

? ? ? ? ? ? stateless: true

? ? ? ? ? ? anonymous: true

? ? ? ? ? ? form_login:

? ? ? ? ? ? ? ? check_path:? ? ? ? ? ? ? ?/api/login_check

? ? ? ? ? ? ? ? username_parameter: email

? ? ? ? ? ? ? ? password_parameter: password

? ? ? ? ? ? ? ? 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

? ? ? ? ? ? guard:

? ? ? ? ? ? ? ?authenticators:

? ? ? ? ? ? ? ? ? ?- lexik_jwt_authentication.jwt_token_authenticator

? ? ? ? access_control:

? ? ? ? ? ? - { path: ^/api/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }

? ? ? ? ? ? - { path: ^/api/, role: IS_AUTHENTICATED_FULLY }

2配置/routes.yaml


api_login_check:

? ? path: /api/login_check

3用curl測試一下


X POST -H "Content-Type: application/json" http://localhost/api/login_check -d '{"email":"[email protected]","password":"pass"}'

查看完整回答
反對 回復 2023-09-22
?
函數式編程

TA貢獻1807條經驗 獲得超9個贊

您不允許匿名訪問任何防火墻。您應該將匿名選項添加到您的主防火墻中。


    main:

        pattern:   ^/

        stateless: true

        anonymous: true

        guard:

            authenticators:

                - lexik_jwt_authentication.jwt_token_authenticator

        provider: doctrine_provider


查看完整回答
反對 回復 2023-09-22
?
阿晨1998

TA貢獻2037條經驗 獲得超6個贊

我發現此升級存在問題,但我有解決方案。


在 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_ttl: '%env(JWT_TTL)%'

    token_extractors:

        authorization_header:

            enabled: true

            prefix:  '%env(JWT_TOKEN_PREFIX)%'

            name:    Authorization

    user_identity_field: email


查看完整回答
反對 回復 2023-09-22
  • 3 回答
  • 0 關注
  • 149 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號