简体   繁体   English

Symfony 4 - 使用 LexikJWTAuthenticationBundle 未找到 JWT

[英]Symfony 4 - JWT not found with LexikJWTAuthenticationBundle

Good afternoon,下午好,

I try to use LexikJWTAuthenticationBundle in my project and I have a problem with the token which is not generated.我尝试在我的项目中使用 LexikJWTAuthenticationBundle,但我遇到了未生成的令牌的问题。 I have set the private & public keys in var/jwt directory.我已经在 var/jwt 目录中设置了私钥和公钥。

The API returns this response when I try use the login route:当我尝试使用登录路由时,API 返回此响应:

{
    "code": 401,
    "message": "JWT Token not found"
}

Apache Virtualhost: Apache 虚拟主机:

<VirtualHost *:80>
    ServerName ypostirixi
    DocumentRoot "/var/www/ypostirixi/public"

    RewriteEngine On
    RewriteCond %{HTTP:Authorization} ^(.*)
    RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
</VirtualHost>

.htaccess file in public directory:公共目录中的 .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 security:安全性.yaml 安全性:

encoders:
    App\Entity\User:
        algorithm: bcrypt
providers:
    doctrine_provider:
        entity:
            class: App\Entity\User
            property: email

firewalls:
    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_provider

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

I expect to successfully use the login route and generate a valid token on the others routes.我希望成功使用登录路由并在其他路由上生成有效令牌。

You are not allowing anonymous access to any firewalls.您不允许匿名访问任何防火墙。 You should add anonymous option to your main firewall.您应该在主防火墙中添加匿名选项。

    main:
        pattern:   ^/
        stateless: true
        anonymous: true
        guard:
            authenticators:
                - lexik_jwt_authentication.jwt_token_authenticator
        provider: doctrine_provider
  • Maybe you forgot to configure firewall of login in section firewalls or login parameters (email as username)..也许您忘记在防火墙或登录参数(电子邮件作为用户名)中配置登录防火墙..

check with this检查这个

1 config/packages/security.yaml 1配置/包/安全.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 config/routes.yaml 2配置/路由.yaml

api_login_check:
    path: /api/login_check

3 Test it with curl 3用 curl 测试它

X POST -H "Content-Type: application/json" http://localhost/api/login_check -d '{"email":"user@example.com","password":"pass"}'

NB: if not working for you, maybe you skipped a step on the configuration or you did not configure the bundle properly, You must see on the documentation https://github.com/lexik/LexikJWTAuthenticationBundle/blob/master/Resources/doc/index.md#installation注意:如果不适合您,也许您跳过了配置步骤或者您没有正确配置捆绑包,您必须在文档https://github.com/lexik/LexikJWTAuthenticationBundle/blob/master/Resources/doc上查看/index.md#安装

Thank you for your help.谢谢您的帮助。

I have discover a problem about this upgrade, but I have a solution.我发现了有关此升级的问题,但我有一个解决方案。

In lexik_jwt_authentication.yaml file:在 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM