我正在构建一个Symfony2项目,但我遇到了一个问题:我配置了 security.yml 和 routing.yml 来创建一个身份验证系统。我有2个捆绑包:一个用于管理员,一个用于用户。当我尝试访问登录页面时,我有一个重定向循环。
这是我的 security.yml 文件:
security:
encoders:
Symfony\Component\Security\Core\User\User:
algorithm: bcrypt
cost: 12
BackOfficeBundle\Entity\Administrateur:
algorithm: bcrypt
BackOfficeBundle\Entity\Collaborateur:
algorithm: bcrypt
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
providers:
administrators:
entity: { class: BackOfficeBundle:Administrateur, property: username }
users:
entity: { class: BackOfficeBundle:Collaborateur, property: email }
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
front_login:
pattern: ^/login$
anonymous: true
back_login:
pattern: ^/login$
anonymous: true
back:
pattern: ^/platform
anonymous: true
provider: administrators
form_login:
login_path: /platform/login
check_path: /platform/login_check
default_target_path: /platform
logout:
path: /platform/logout
target: /platform/login
front:
pattern: ^/collaborateur
anonymous: false
provider: users
form_login:
login_path: /collaborateur/login
check_path: /collaborateur/login_check
default_target_path: /collaborateur
logout:
path: /collaborateur/logout
target: /collaborateur/clogin
access_control:
#- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }
- { path: ^/platform, roles: ROLE_ADMIN }
- { path: ^/collaborateur, roles: ROLE_USER }
这是 app / config / routing.yml :
front_office:
resource: "@FrontOfficeBundle/Resources/config/routing.yml"
prefix: /collaborateur
back_office:
resource: "@BackOfficeBundle/Resources/config/routing.yml"
prefix: /platform
和 BackOfficeBundle / Resources / config / routing.yml :
login:
pattern: /login
defaults: { _controller: UserBundle:Security:login }
login_check:
pattern: /login_check
logout:
pattern: /logout
答案 0 :(得分:1)
您需要在security.yml
中描述登录路线是公开的,如下所示:
access_control:
- { path: ^/platform/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/collaborateur/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/platform, roles: ROLE_ADMIN }
- { path: ^/collaborateur, roles: ROLE_USER }
希望这个帮助
答案 1 :(得分:1)
试试这个:
firewalls:
...
front_login:
pattern: ^/platform/login$
anonymous: true
back_login:
pattern: ^/collaborateur/login$
anonymous: true
...
您的登录表单位于/platform/login
,但您的匿名安全例外仅适用于/login
(这是错误的),^/platform
下的所有内容都受到保护。 Symfony检测安全区域并尝试重定向到登录路径,但/platform/login
再次处于安全区域(再次,再次)。