Symfony 4重定向循环以多种形式登录

时间:2019-04-25 05:55:42

标签: symfony security login redirect-loop

我正在设置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登录。

1 个答案:

答案 0 :(得分:0)

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