Symfony 角色和安全性解释
Symfony Role and security explained
我正在尝试学习 Symfony 角色和安全性。我当前的 security.yml 文件看起来像这样:
role_hierarchy:
ROLE_USER: ROLE_DO_ALMOST_NOTHING
ROLE_EDITOR: [ ROLE_USER, ROLE_ALLOWED_TO_EDIT ]
ROLE_CONTRIBUTOR: [ ROLE_EDITOR, ROLE_ALLOWED_TO_CONTRIBUTE ]
ROLE_ADMIN: [ ROLE_CONTRIBUTOR ]
ROLE_SUPER_ADMIN: [ ROLE_ADMIN, ROLE_ALLOWED_TO_DO_ANY_THING ]
access_control:
- { path: ^/admin, roles: ROLE_USER }
- { path: ^/admin/editor, roles: ROLE_ADMIN }
- { path: ^/editor, roles: ROLE_EDITOR }
- { path: ^/contributor, roles: ROLE_CONTRIBUTOR }
- { path: ^/super, roles: ROLE_SUPER_ADMIN }
我正在为我的用户使用此设置:
providers:
in_memory:
memory:
users:
person:
password: password!
roles: 'ROLE_USER'
admin:
password: password2
roles: 'ROLE_ADMIN'
这是我的问题。我一直缺少安全性的 access_control 部分,但是,角色标记为 ROLE_ADMIN 的路径 ^/admin/editor
将允许用户person 访问路由,即使 person 用户没有 ROLE_ADMIN 的角色。我想知道这是否是因为路由本身与 ^admin
路由由同一个控制器共享?或者有人看到我的代码哪里出了问题,因为用户可以访问他们不应该访问的路由。
其他路线:
- { path: ^/editor, roles: ROLE_EDITOR }
- { path: ^/contributor, roles: ROLE_CONTRIBUTOR }
- { path: ^/super, roles: ROLE_SUPER_ADMIN }
按预期工作。
问题是您在匹配 admin/editor
之前匹配 /admin
,这只需要 ROLE_USER 角色。当你有:
- { path: ^/admin, roles: ROLE_USER }
匹配以 /admin
开头的所有内容,包括 admin/editor
。一旦 Symfony 找到合适的路由,它就不会检查第一个。
所以你的 ^/admin/editor/
支票永远不会到达。试试这个:
access_control:
- { path: ^/admin/editor, roles: ROLE_ADMIN }
- { path: ^/admin, roles: ROLE_USER }
- { path: ^/editor, roles: ROLE_EDITOR }
- { path: ^/contributor, roles: ROLE_CONTRIBUTOR }
- { path: ^/super, roles: ROLE_SUPER_ADMIN }
根据经验,您最 granular/specific 的路线应该放在第一位。任何子路线都应始终放在主路线之前。
我正在尝试学习 Symfony 角色和安全性。我当前的 security.yml 文件看起来像这样:
role_hierarchy:
ROLE_USER: ROLE_DO_ALMOST_NOTHING
ROLE_EDITOR: [ ROLE_USER, ROLE_ALLOWED_TO_EDIT ]
ROLE_CONTRIBUTOR: [ ROLE_EDITOR, ROLE_ALLOWED_TO_CONTRIBUTE ]
ROLE_ADMIN: [ ROLE_CONTRIBUTOR ]
ROLE_SUPER_ADMIN: [ ROLE_ADMIN, ROLE_ALLOWED_TO_DO_ANY_THING ]
access_control:
- { path: ^/admin, roles: ROLE_USER }
- { path: ^/admin/editor, roles: ROLE_ADMIN }
- { path: ^/editor, roles: ROLE_EDITOR }
- { path: ^/contributor, roles: ROLE_CONTRIBUTOR }
- { path: ^/super, roles: ROLE_SUPER_ADMIN }
我正在为我的用户使用此设置:
providers:
in_memory:
memory:
users:
person:
password: password!
roles: 'ROLE_USER'
admin:
password: password2
roles: 'ROLE_ADMIN'
这是我的问题。我一直缺少安全性的 access_control 部分,但是,角色标记为 ROLE_ADMIN 的路径 ^/admin/editor
将允许用户person 访问路由,即使 person 用户没有 ROLE_ADMIN 的角色。我想知道这是否是因为路由本身与 ^admin
路由由同一个控制器共享?或者有人看到我的代码哪里出了问题,因为用户可以访问他们不应该访问的路由。
其他路线:
- { path: ^/editor, roles: ROLE_EDITOR }
- { path: ^/contributor, roles: ROLE_CONTRIBUTOR }
- { path: ^/super, roles: ROLE_SUPER_ADMIN }
按预期工作。
问题是您在匹配 admin/editor
之前匹配 /admin
,这只需要 ROLE_USER 角色。当你有:
- { path: ^/admin, roles: ROLE_USER }
匹配以 /admin
开头的所有内容,包括 admin/editor
。一旦 Symfony 找到合适的路由,它就不会检查第一个。
所以你的 ^/admin/editor/
支票永远不会到达。试试这个:
access_control:
- { path: ^/admin/editor, roles: ROLE_ADMIN }
- { path: ^/admin, roles: ROLE_USER }
- { path: ^/editor, roles: ROLE_EDITOR }
- { path: ^/contributor, roles: ROLE_CONTRIBUTOR }
- { path: ^/super, roles: ROLE_SUPER_ADMIN }
根据经验,您最 granular/specific 的路线应该放在第一位。任何子路线都应始终放在主路线之前。