Symfony 4 重定向循环以登录多个角色

Symfony 4 redirect loop to login form multiple role

我正在设置 Symfony 4 来创建新网站,但是当我想用具有多个角色 ROLE_USERROLE_ADMIN 的用户登录时,我被重定向到登录页面。只有一个角色 ROLE_USER 我可以登录,如何解决这个问题?

配置是 PHP 7.2,Symfony 4.2,Web 服务器内置 Symfony "server:start"。 我尝试更改安全配置但没有任何改变。

security.yaml

security:
    encoders:
        App\Entity\User: plaintext

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: ROLE_ADMIN

    # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
    providers:
        fos_userbundle:
            id: fos_user.user_provider.username_email

    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false

        secured_area:
            # this firewall applies to all URLs
            pattern: ^/

            # but the firewall does not require login on every page
            # denying access is done in access_control or in your controllers
            anonymous: ~

            # This allows the user to login by submitting a username and password
            # Reference: http://symfony.com/doc/current/cookbook/security/form_login_setup.html
            form_login:
                # fos user bundle handles the form login
                #provider: fos_userbundle
                # The route name that the login form submits to
                check_path: fos_user_security_check
                # The name of the route where the login form lives
                # When the user tries to access a protected page, they are redirected here
                login_path: fos_user_security_login
                # Secure the login form against CSRF
                # Reference: http://symfony.com/doc/current/cookbook/security/csrf_in_login_form.html
                csrf_token_generator: security.csrf.token_manager

            logout:
                # The route name the user can go to in order to logout
                path: fos_user_security_logout
                # The name of the route to redirect to after logging out
                target: homepage

    # Easy way to control access for large sections of your site
    # Note: Only the *first* access control that matches will be used
    access_control:
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/categories, role: ROLE_ADMIN }
        - { path: ^/tags, role: ROLE_ADMIN }
        - { path: ^/typewords, role: ROLE_ADMIN }
        - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin, role: ROLE_ADMIN }
        - { path: ^/, role: ROLE_USER }

routes.yaml

controllers:
    resource: '../src/Controller/'
    type: annotation

fos_user:
    resource: "@FOSUserBundle/Resources/config/routing/all.xml"

easy_admin_bundle:
    resource: '@EasyAdminBundle/Controller/EasyAdminController.php'
    prefix: /admin
    type: annotation

我希望使用另一个 ROLE 登录而不是 ROLE_USER

按层次结构,ROLE_ADMIN 的用户自动拥有 ROLE_USER。因此,只需从该用户中删除 ROLE_USER 即可。不确定角色是如何从用户提供者加载的,还要检查 ROLE_ADMIN 在数据库中的写入方式或用于映射用户实体的内容。 (包括用户实体的映射文件以进一步了解)