【发布时间】:2012-03-26 12:59:34
【问题描述】:
我在 symfony2 中的安全配置有问题。我需要两个防火墙用于两个不同的用户实体。
这是我的配置文件:
security.yml:
security:
encoders:
entity_owner:
class: Pisteur\CoreBundle\Entity\OwnerAccount
algorithm: sha512
iterations: 5000
encode_as_base64: false
entity_business:
class: Pisteur\BusinessBundle\Entity\BusinessOwner
algorithm: sha512
iterations: 5000
encode_as_base64: false
providers:
entity_owner:
name: entity_owner
entity:
class: Pisteur\CoreBundle\Entity\OwnerAccount
property: username
entity_business:
name: entity_business
entity:
class: Pisteur\BusinessBundle\Entity\BusinessOwner
property: username
firewalls:
entity_business:
pattern: ^/business
anonymous: ~
form_login:
check_path: /business/login_check
login_path: /business/login
default_target_path: /business/dashboard
provider: entity_business
logout:
path: /logout
target: /business/login
entity_owner:
pattern: ^/
anonymous: ~
form_login:
login_path: /login
check_path: /login_check
default_target_path: /dashboard
provider: entity_owner
logout:
path: /logout
target: /login
role_hierarchy:
ROLE_ADMIN: [ROLE_USER, ROLE_BUSINESS]
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/business/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/account, roles: ROLE_USER }
- { path: ^/dashboard, roles: ROLE_USER }
- { path: ^/business/dashboard, roles: ROLE_BUSINESS }
- { path: ^/_internal, roles: IS_AUTHENTICATED_ANONYMOUSLY, ip: 127.0.0.1 }
这是我所有的路线:
security_login:
pattern: /login
defaults: { _controller: "PisteurSecurityBundle:Security:login" }
requirements: { _method: get }
login_check:
pattern: /login_check
business_security_login:
pattern: /business/login
defaults: { _controller: "PisteurSecurityBundle:BusinessSecurity:login" }
requirements: { _method: get }
business_login_check:
pattern: /business/login_check
logout:
pattern: /logout
OwnerAccount 的登录表单:
<form id="login-form" action="{{ path('login_check') }}" method="post">
<label><input id="username" type="text" name="_username" /></label>
<label><input id="password" type="password" name="_password" /></label>
<button class="btn custom large orange-button" type="submit" id="login-button">{% trans from "login" %}login{% endtrans %}</button>
</form>
BusinessOwner 的登录表单:
<form id="login-form" action="{{ path('business_login_check') }}" method="post">
<label><input id="username" type="text" name="_username" /></label>
<label><input id="password" type="password" name="_password" /></label>
<button class="btn custom large orange-button" type="submit" id="login-button">{% trans from "login" %}login{% endtrans %}</button>
</form>
当我使用 OwnerAccount 表单登录时,它可以工作并将我重定向到 /dashboard。 当我使用 BusinessOwner 表单登录时,它不起作用并重定向到 /login(应该是 /business/login)并出现错误“BadCredentials”
我不知道为什么,但似乎只使用了 entity_owner(因为它从 /business/login 重定向到 /login)
我的配置有问题吗?
【问题讨论】:
-
您的 entity_business 模式与您的 entity_owner 模式匹配,(即 ^/business 与 ^/ 匹配)。我认为它们必须是完全独立的模式才能正常工作。
-
这可能是真的。我不太擅长正则表达式。对于我的 entity_owner,匹配“^/”但不匹配“^/business”的正则表达式是什么?谢谢
-
选项 1:为 entity_owner 模式尝试类似 ^/(?!business) 的方法。选项2:将 entity_owner 模式更改为 ^/owner,并将主页重定向到 /owner。
-
你应该把它写成官方答案。我稍后会测试它,如果它有效,我会将您的答案标记为好的答案。