Symfony3 应用程序中缺少 security.context 服务

问题描述

我继承了一个 Symfony 3 应用程序,该应用程序似乎具有良好的 FOSUserBundle 安装。但是,当我尝试将 @security.context 注入服务以检索登录用户时,我收到有关 @security.context 缺少服务的错误消息。我尝试了在其他几个线程上看到的检查——确保 services.yml 已填写并确保它正确包含在我的主配置文件中——看起来一切都正确完成。这是我的 security.yml 文件

security:
    providers:
        fos_userbundle:
            id: fos_user.user_provider.username
        api_key_user_provider:
            id: security.user.provider.api_key

    encoders:
        FOS\UserBundle\Model\UserInterface: sha512

    firewalls:
        # disables authentication for assets and the profiler,adapt it according to your needs
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false

        # -> custom firewall for the admin area of the URL
        admin:
            pattern:            /admin(.*)
            context:            admin
            form_login:
                provider:       fos_userbundle
                login_path:     /admin/login
                use_forward:    false
                check_path:     /admin/login_check
                failure_path:   null
            logout:
                path:           /admin/logout
                target:         /admin/login
            anonymous:          true
            switch_user:        true
            remember_me:
                secret:      '%secret%'
                lifetime: 604800 # 1 week in seconds
                path:     /

        # -> end custom configuration

        hwi:
            pattern: ^/hwi
            form_login: false
            oauth:
                resource_owners:
                    twitter:  "/hwi/check-twitter"
                    instagram:  "/hwi/check-instagram"
                    google:  "/hwi/check-google"
                    facebook:  "/hwi/check-facebook"
                    apple: "/hwi/check-apple"
                login_path:   "/hwi/login"
                check_path:   "/hwi/check"
                use_forward:  false
                failure_path: "/hwi/login"
                oauth_user_provider:
                    service: app.service.hwi


        api:
            pattern: ^/api/
            form_login: false
            stateless: true
            anonymous: true
            simple_preauth:
                authenticator: 'api_key_authenticator'
            provider: api_key_user_provider
            entry_point: api_key_authenticator


    access_control:
        # URL of FOSUserBundle which need to be available to anonymous users
        - { path: ^/api/login,role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/api/register,role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/api/doc,role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/resetting,role: IS_AUTHENTICATED_ANONYMOUSLY }

        # Admin login page needs to be access without credential
        - { path: ^/admin/login$,role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/logout$,role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/login_check$,role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/resetting,role: IS_AUTHENTICATED_ANONYMOUSLY }

        # Secured part of the site
        # This config requires being logged for the whole site and having the admin role for the admin part.
        # Change these rules to adapt them to your needs
        - { path: ^/admin/,role: [ROLE_SONATA_ADMIN] }
        - { path: ^/.*,role: IS_AUTHENTICATED_ANONYMOUSLY }

登录系统运行良好,其他操作如注销、重置密码等也运行良好。

但是缺少 @security.context

我做错了什么?该服务是否已被弃用?

解决方法

我想我发现了我应该在较新版本的 Symfony 中做什么。

看来我可以使用 @security.token_storage 来获取令牌(以及用户)。