Symfony 4:FOSUserBundle:覆盖控制器

Symfony 4: FOSUserBundle : Override Controller

我想把管理员登录页面和用户登录页面分开。我使用从某些来源获得的代码以这种方式进行操作,但出现错误。

// config/security.yaml

security:
    # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
    encoders: ...

role_hierarchy: ...


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

firewalls:
    dev:
        ...
    admin:
        pattern:            /admin(.*)
        form_login:
            provider:       fos_userbundle
            login_path:     /admin/login
            check_path:     /admin/login_check
            default_target_path: /admin/
        logout:
            path:           /admin/logout
            target:         /admin/login
        anonymous:    true
    main:
        pattern: ^/
        form_login:
            provider: fos_userbundle
            csrf_token_generator: security.csrf.token_manager

        logout:       true
        anonymous:    true

# 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: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/admin/, role: ROLE_ADMIN }
    - { path: ^/admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/admin/logout$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/admin/login_check$, role: IS_AUTHENTICATED_ANONYMOUSLY }

routes.yaml文件如下

admin_login:
  path:  /admin/login
  defaults: { _controller: App\UserBundle\Controller\SecurityController }

并且SecurityController文件如下。

<?php

namespace App\UserBundle\Controller;

use FOS\UserBundle\Controller\SecurityController as BaseController;

class SecurityController extends BaseController
{

    public function renderLogin(array $data)
    {
        $requestAttributes = $this->container->get('request')->attributes;

        if ('admin_login' === $requestAttributes->get('_route')) {
            $template = sprintf('admin/Security/login.html.twig');
        } else {
            $template = sprintf('@FOSUser/Security/login.html.twig');
        }

        return $this->container->get('templating')->renderResponse($template, $data);
    }
}

我是这样写的,但是当我进入管理/登录页面时,出现如下错误。

This page isn’t working
127.0.0.1 redirected you too many times.

ERR_TOO_MANY_REDIRECTS

我该如何解决这个错误。

您的重定向过多,因为 symfony 从上到下读取并且 /admin/ 写在 /admin/login/admin/login_check 之前,因此它将与此访问控制相匹配。

Access control (Symfony):

For each incoming request, Symfony checks each access_control entry to find one that matches the current request. As soon as it finds a matching access_control entry, it stops - only the first matching access_control is used to enforce access.

因此您需要将 /admin_login 放在 /admin

之前

更新您的访问控制,例如:

access_control:
  - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
  - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
  - { path: ^/admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  - { path: ^/admin/logout$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  - { path: ^/admin/login_check$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  - { path: ^/admin/, role: ROLE_ADMIN }