I'm trying to install and configure lexik/LexikJWTAuthenticationBundle
in symfony 5.2 project.
When I applied the following documentation https://github.com/lexik/LexikJWTAuthenticationBundle/blob/master/Resources/doc/index.md#installation, it gave me this error:
You must either configure a "public_key" or a "secret_key".
So, I applied another configuration :
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)%'
and in my .env file I have this :
###> lexik/jwt-authentication-bundle ###
JWT_SECRET_KEY=%kernel.project_dir%/config/jwt/private.pem
JWT_PUBLIC_KEY=%kernel.project_dir%/config/jwt/public.pem
JWT_PASSPHRASE=mypassphrase
JWT_TTL=3600 # 1 hour expiration
###< lexik/jwt-authentication-bundle ###
Then I ran these commands to generate the keys (the keys are generated successfuly under my directory config/jwt
):
- openssl genpkey -out config/jwt/private.pem -aes256 -algorithm rsa -pkeyopt rsa_keygen_bits:4096
- openssl pkey -in config/jwt/private.pem -out config/jwt/public.pem -pubout
But, when I run this command to check if it works, I have this error :
curl -X POST -H "Content-Type: application/json" http://localhost:8000/api -d '{"username":"johndoe","password":"test"}'
"An error occurred","status":400,"detail":"Invalid JSON.","class":"Symfony\Component\HttpKernel\Exception\BadRequestHttpException
And I obtain this error when I test this method in my controller as explained here: https://github.com/lexik/LexikJWTAuthenticationBundle/blob/master/Resources/doc/7-manual-token-creation.md :
"Cannot autowire argument $user of "App\Controller\DefaultController::getTokenUser()": it references interface "Symfony\Component\Security\Core\User\UserInterface" but no such service exists. Did you create a class that implements this interface?"
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Security\Core\User\UserInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
class ApiController extends Controller
{
public function getTokenUser(UserInterface $user, JWTTokenManagerInterface $JWTManager)
{
// ...
return new JsonResponse(['token' => $JWTManager->create($user)]);
}
}
Is this bundle compatible with Symfony 5.2 and how can I resolve this problem ?