LexikJWTAuthenticationBundle: Cannot extend JWTAuthenticator and override loadUser method
Hi there!
I use this bundle in my symfony 6 project to authenticate my users by jwt token. Everything is going fine until I want to create a custom authenticator to add some logic in how I authenticate my users. After following the documentation https://github.com/lexik/LexikJWTAuthenticationBundle/blob/2.x/Resources/doc/6-extending-jwt-authenticator.rst, my authenticator is never called and I don’t have any error either.
In my services.yaml file
# config/services.yaml
services:
app.jwt_custom_api_authenticator:
class: App\Http\Api\Security\Authenticator\JWTCustomApiAuthenticator
parent: lexik_jwt_authentication.security.jwt_authenticator
In security.yaml file
# config/packages/security.yaml
security:
enable_authenticator_manager: true
password_hashers:
Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
App\Entity\User:
algorithm: auto
providers:
app_user_provider:
entity:
class: App\Entity\User
property: email
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
login:
pattern: ^/api/auth
stateless: true
json_login:
username_path: email
check_path: api_login_check
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
refresh_jwt:
check_path: api_refresh_token
provider: app_user_provider
api:
pattern: ^/api
stateless: true
jwt:
authenticator: app.jwt_custom_api_authenticator
main:
lazy: true
provider: app_user_provider
In my App\Http\Api\Security\Authenticator\JWTCustomApiAuthenticator file
<?php
namespace App\Http\Api\Security\Authenticator;
use App\Entity\User;
use Lexik\Bundle\JWTAuthenticationBundle\Security\Authenticator\JWTAuthenticator;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
use Symfony\Component\Security\Core\User\UserInterface;
class JWTCustomApiAuthenticator extends JWTAuthenticator
{
protected function loadUser(array $payload, string $identity): UserInterface
{
/** @var UserInterface|User $user */
$user = parent::loadUser($payload, $identity);
if ($user->isBlocked()){
$ex = new UserNotFoundException('Your account has been deactivated by the administrators');
$ex->setUserIdentifier($identity);
throw $ex;
}
return $user;
}
}
SYMFONY 6 + PHP 8.1
I would like to know how to implement my authenticator logic.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 24
It’s work for me fine and the jwt is not generated. I think this is the best solution to do this instead of to override the
JWTAuthenticatorlaodUsermethod.In security.yaml