You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Symfony 6.3自定义AccessToken Handler报错:父定义不存在

问题解决:自定义AccessToken Handler报错“Parent definition不存在”

错误原因

你的配置文件里指定的令牌处理器命名空间是Api\Security\AccessTokenHandler,但实际代码中的类命名空间是App\Security\AccessTokenHandler,两者不匹配,导致Symfony找不到对应的服务定义。

修复步骤

  1. 修正security.yaml中的命名空间
    将token_handler的值改为实际的类命名空间:

    security:
        password_hashers:
            Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
        providers:
            users_in_memory: { memory: null }
        firewalls:
            dev:
                pattern: ^/(_(profiler|wdt)|css|images|js)/
                security: false
            api:
                pattern: ^/api
                stateless: true
                access_token:
                    token_handler: App\Security\AccessTokenHandler
    
  2. 确认服务自动注册(可选)
    Symfony 6+默认会自动注册App\命名空间下的类作为服务,且如果你的类实现了AccessTokenHandlerInterface,autoconfigure会自动配置相关服务标签。如果项目修改过服务自动注册规则,需要确保在services.yaml中手动注册:

    services:
        App\Security\AccessTokenHandler:
            autoconfigure: true
    
  3. 清除缓存
    执行缓存清除命令确保配置生效:

    php bin/console cache:clear
    

内容的提问来源于stack exchange,提问作者Nuryagdy Mustapayev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 07:13:16