简体   繁体   English

Symfony2中具有相同模式的多个安全防火墙

[英]Multiple security firewalls with the same pattern in Symfony2

I'm using two firewalls with same pattern for two types of users: 我正在为两种类型的用户使用两个具有相同模式的防火墙:

  • Admin with access to both frontend and backend who can see some extra controls in frontend app then normal user. 可以访问前端和后端的管理员,他们可以看到前端应用程序中的某些额外控件,然后是普通用户。
  • User who can access only frontend. 只能访问前端的用户。

This is my simplified security.yml configuration: 这是我简化的security.yml配置:

firewalls:
    admin:
        pattern: .*
        form_login:
            login_path: /admin/login
            check_path: /admin/login
        logout:
            path:   /admin/logout
        ...

    front:
        pattern: .*
        form_login:
            login_path: /user/login
            check_path: /user/login-check
        logout: true
        anonymous: true
        ...

    dev:
        pattern:  ^/(_(profiler|wdt)|css|images|js)/
        security: false


access_control:
    - { path: ^/admin/, role: ROLE_ADMIN }
    - { path: ^/admin/login, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/user/login, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/user/profile, role: ROLE_FRONTEND_USER }
    - { path: ^/user/upload-photo, role: ROLE_FRONTEND_USER }
    ...

The problem is that when any non loggedin user tries to access for example /user/profile they're redirected to /admin/login . 问题是,当任何非登录用户尝试访问例如/user/profile他们将被重定向到/admin/login I Guess this is because there's no connection between firewall and access_control so Symfony can't know if the user tried to access section for ROLE_FRONTEND_USER or ROLE_ADMIN and then redirected accordingly. 我猜这是因为firewallaccess_control之间没有连接,所以Symfony无法知道用户是否尝试访问ROLE_FRONTEND_USERROLE_ADMIN ,然后进行相应的重定向。

My question is, is there any elegant way to solve this? 我的问题是,有什么优雅的方法可以解决这个问题吗? Maybe use an event listener and manually check on denied permissions whether the requested URL required ROLE_FRONTEND_USER or ROLE_ADMIN roles? 也许使用事件侦听器并手动检查拒绝的权限,那么所请求的URL是否需要ROLE_FRONTEND_USERROLE_ADMIN角色?

You can manually set the url where the user will be redirected : 您可以手动设置将用户重定向到的网址:

front:
    pattern: .*
    # ...
    access_denied_url: /user/login

Anonymous users will be redirected to the expected login route without need of a specific pattern per firewall. 匿名用户将被重定向到预期的登录路由,而无需每个防火墙使用特定的模式。

Hope this solves your problem. 希望这能解决你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM