多个防火墙提升了Symfony 5.4的安全性
P粉952365143
P粉952365143 2023-08-26 16:14:30
[PHP讨论组]

我正在尝试将用户的身份验证与管理员的身份验证分开。

所以我创建了2个防火墙和2个不同的访问控制。

我的security.yaml文件如下:

enable_authenticator_manager: true
  password_hashers:
    Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
  providers:
    owner_authentication:
      entity:
        class: App\Entity\Merchant\Owner
        property: emailAddress
    user_authentication:
      entity:
        class: App\Entity\User\User
        property: emailAddress
  firewalls:
    dev:
      pattern: ^/(_(profiler|wdt)|css|images|js)/
      security: false
    user:
      lazy: true
      pattern: ^/admin/login/
      provider: user_authentication
      user_checker: App\Security\AuthentificableModelChecker
      form_login:
        provider: user_authentication
        default_target_path: app.dashboard.index
        use_referer: true
        use_forward: false
        login_path: app.authorization.admin_login
        check_path: app.authorization.admin_login
        username_parameter: login[emailAddress]
        password_parameter: login[password]
      logout:
        path: app.authorization.logout
        target: app.authorization.admin_login
    main:
      lazy: true
      pattern: ^/
      provider: owner_authentication
      user_checker: App\Security\AuthentificableModelChecker
      form_login:
        provider: owner_authentication
        default_target_path: app.dashboard.index
        use_referer: true
        use_forward: false
        login_path: app.authorization.login
        check_path: app.authorization.login
        username_parameter: login[emailAddress]
        password_parameter: login[password]
      logout:
        path: app.authorization.logout
        target: app.authorization.login
  access_control:
    - { path: ^/admin/login, roles: PUBLIC_ACCESS}
    - { path: ^/login, roles: PUBLIC_ACCESS }
    - { path: ^/, roles: ROLE_USER }

主防火墙上的一切都正常工作,但当我使用用户(管理员)防火墙提交按钮时,登录页面会刷新自身,然后什么也不发生。我没有任何错误。

** 如果我在主防火墙上添加用户(管理员)登录,那么/admin/login将正常工作,而另一个则不再工作。

当我调用$authenticationUtils->getLastAuthenticationError()时,我没有收到任何错误。但验证也不起作用。

这是我的控制器的样子:

public function adminLogin(AuthenticationUtils $authenticationUtils): Response
    {
        if ($this->getUser()) {
            return $this->redirectToRoute('app.dashboard.index');
        }
        
        $loginForm = $this->createForm(LoginType::class, ['emailAddress' => $authenticationUtils->getLastUsername()]);


        return $this->renderForm('app/pages/authorization/admin_login.html.twig', [
            'title'      => 'Log in',
            'login_form' => $loginForm,
            'error'      => $authenticationUtils->getLastAuthenticationError()?->getMessageKey(),
        ]);
    }

这是一个人遇到的相同问题:https://grafikart.fr/forum/35234,但我找不到任何解决方案。

P粉952365143
P粉952365143

全部回复(1)
P粉041881924

最后,我找到了答案,我会在这里发布:https://stackoverflow.com/a/42352112/8003007 我所需要做的是在两个防火墙中添加一个context: my_context

这是一个难以识别的选项,因为它在官方和当前的Symfony文档中并没有出现,只出现在之前的版本中,比如Symfony 3.4。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号