2

我在 symfony 3 中的系统登录有问题。所以我security.yml的是:

security:
role_hierarchy:
  ROLE_ADMIN:       ROLE_USER
  ROLE_FMTI:        ROLE_FMTI
  ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
firewalls:
  dev:
      pattern:  ^/(_(profiler|wdt)|css|images|js)/
      security: false
  secured_area:
      pattern:    ^/admin
      anonymous: ~
      form_login:
          always_use_default_target_path: true
          default_target_path: /admin/homepage
          login_path:  /admin/login
          check_path:  /admin/login_check
      logout:
          path:   /admin/logout
          invalidate_session: true
          target: /admin/login
  access_control:
  - { path: ^/admin/homepage, roles: ROLE_ADMIN }

 providers:
  in_memory:
      memory:
          users:
              admin: { password: admin, roles: 'ROLE_ADMIN' }

 encoders:
  Symfony\Component\Security\Core\User\User: plaintext

路由:

app_admin_homepage:
  path:     /homepage
  defaults: { _controller: AppAdminBundle:Login:index }
login:
  path:   /login
  defaults:  { _controller: AppAdminBundle:Login:login }
login_check:
  path:   /login_check
logout:
  path: /logout

以及来自 LoginController 的方法 login :

public function loginAction(Request $request)
{
    $authenticationUtils = $this->get('security.authentication_utils');
    // get the login error if there is one
    $error = $authenticationUtils->getLastAuthenticationError();
    // last username entered by the user
    $lastUsername = $authenticationUtils->getLastUsername();

    return $this->render('AppAdminBundle:Member:login.html.twig', array(
        'last_username' => $lastUsername,
        'error'         => $error,
    ));
}

问题是,如果我使用凭据登录应用程序:admin/admin。之后我进行注销。

  1. 如果我尝试访问test.dev/admin/homepage----> 我正在重定向 vers 登录页面test.dev/admin/login,所以很好,我登录为admin.

  2. 如果我尝试访问test.dev/admin/news/all-----> 我可以在不登录的情况下访问此页面,并且我登录为anonymous

/admin/*因此,如果用户未经过身份验证,我想重定向到所有路由的登录页面。谢谢,对不起我的英语

4

1 回答 1

2

在访问控制中,您需要添加:

access_control:
  - { path: ^/admin/, roles: ROLE_ADMIN }

这意味着对于 /admin/ 路由之外的任何内容,都需要 ROLE_ADMIN。

- 更新

如果您需要访问/admin/login/,则需要添加到除/login 之外的每个管理路由,如/admin/api/ 之类的路径模式,因此在您的访问控制中,您将拥有以下内容:

access_control:
      - { path: ^/admin/api/, roles: ROLE_ADMIN }
于 2015-12-29T16:18:21.087 回答