Skip to content
Advertisement

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. I have set the private & public keys in var/jwt directory.

The API returns this response when I try use the login route :

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

Apache Virtualhost :

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

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

encoders:
    AppEntityUser:
        algorithm: bcrypt
providers:
    doctrine_provider:
        entity:
            class: AppEntityUser
            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.

Advertisement

Answer

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:
    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
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement