修改LexikJWTAuthenticationBundle默认用户名字段适配Symfony升级
适配LexikJWTAuthenticationBundle兼容旧版
_username字段(Symfony5.4升级) 要让Symfony5.4版本的LexikJWTAuthenticationBundle兼容旧版的_username字段(替代默认的username),只需在security.yaml的登录防火墙配置中,给form_login添加字段名覆盖参数即可:
修改后的security.yaml配置
firewalls: login: pattern: ^/api/login stateless: true anonymous: true form_login: check_path: /api/login_check success_handler: lexik_jwt_authentication.handler.authentication_success failure_handler: lexik_jwt_authentication.handler.authentication_failure require_previous_session: false # 添加以下两行,覆盖默认字段名 username_parameter: "_username" password_parameter: "_password" refresh: pattern: ^/api/token/refresh stateless: true anonymous: true api: pattern: ^/api/secure stateless: true guard: authenticators: - word.user.guard.jwt_token_authenticator main: pattern: ^/ form_login: provider: fos_userbundle csrf_token_generator: security.csrf.token_manager logout: true anonymous: true
说明
Symfony5.4中,LexikJWT的登录认证依赖Symfony原生的form_login组件,该组件默认使用username和password作为请求字段名。通过配置username_parameter和password_parameter,可以强制指定为旧版本使用的_username和_password,从而实现新旧客户端的兼容,无需修改原有API调用逻辑。
内容的提问来源于stack exchange,提问作者oracle972
相关产品推荐
相关产品推荐

