2012-03-09 166 views
2

我在symfony2中的安全配置有問題。我需要兩個不同用戶實體的防火牆。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> 

登錄f ORM的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形式登錄,它的工作原理和重定向我對/儀表板。 當我用BusinessOwner表單登錄時,它不工作,並重定向到/登錄(應該是/業務/登錄)與錯誤「BadCredentials」

我不知道爲什麼,但似乎只有entity_owner是使用(因爲它重定向到/登錄/業務/登錄)

這是在我的配置錯了嗎?

+0

您的entity_business模式與您的entity_owner模式匹配(即^/business與^ /匹配)。我認爲他們必須是完全獨立的模式才能正常工作。 – Martin 2012-03-12 12:54:44

+0

這可能是真的。正則表達式我不太擅長。正則表達式匹配「^ /」而不是「^/business」爲我的entity_owner?謝謝 – 2012-03-12 13:22:41

+1

選項1:爲entity_owner模式嘗試類似^ /(?! business)。選項2:將entity_owner模式更改爲^/owner,並將主頁面重定向到/ owner。 – Martin 2012-03-12 13:36:12

回答

3

爲entity_owner模式嘗試類似^/(?!business),這可能會阻止entity_owner模式匹配entity_business模式。

2

將您的所有者資源移至/所有者。將entity_owner模式更改爲^/owner,並使主頁面重定向到/所有者。