在symfony 3中对登录系统进行了安全保护


Securized the login system in symfony 3

我在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登录的方法:

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---->我正在重定向登录页面,该页面是test.dev/admin/login,所以这很好,我以admin的身份登录。

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

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

在访问控制中,您需要添加以下内容:

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

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

--更新

如果你需要访问/admin/login/,你需要添加到除/login之外的每个管理路由,一个类似/admin/api/的路径模式,所以在你的访问控制中,你会有这个:

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