fosuserBundle多个防火墙检查路径配置

时间:2015-05-01 09:50:49

标签: symfony fosuserbundle

我正在使用Symfony2 2.3和FosUserBundle。我正在尝试创建两个防火墙。一个用于管理部分,另一个用于前端。前端防火墙工作正常,但管理员防火墙不起作用,它显示此错误You must configure the check path to be handled by the firewall using form_login in your security firewall configuration.
这是我的代码:
security.yml

# app/config/security.yml
security:
    encoders:
        FOS\UserBundle\Model\UserInterface: sha512

role_hierarchy:
    ROLE_ADMIN:       ROLE_USER
    ROLE_SUPER_ADMIN: ROLE_ADMIN

providers:
    fos_userbundle:
        id: fos_user.user_provider.username

 # Par-feu
firewalls: 
    # Par-feu du Backoffice
    admin_secured_area:
        pattern: ^/admin/
        form_login:
            provider: fos_userbundle
            csrf_provider: form.csrf_provider
            login_path: fos_admin_user_security_login
            check_path: fos_admin_user_security_check
            default_target_path: biginfo_admin_homepage # A modifier avec le nom de votre route d'accueil pour le Backoffice
        logout:
            path: fos_admin_user_security_logout
            target: fos_admin_user_security_login
        anonymous: true

    # Par-feu du FrontOffice
    front_secured_area:
        pattern: ^/
        form_login:
            provider: fos_userbundle
            csrf_provider: form.csrf_provider
            login_path: fos_user_security_login
            check_path: fos_user_security_check
        logout:
            path:        /logout
            target:      /
        anonymous:       true

access_control:
    - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/admin/, role: ROLE_ADMIN }               

SecurityController.php

    <?php

namespace Biginfo\UserBundle\Controller;

use FOS\UserBundle\Controller\SecurityController as BaseController;

class SecurityController extends BaseController {

    /**
     * Renders the login template with the given parameters. Overwrite this function in
     * an extended controller to provide additional data for the login template.
     *
     * @param array $data
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    protected function renderLogin(array $data) {
        $requestAttributes = $this->container->get('request')->attributes;

        /*
         * Teste de la route de provenance
         * Si route Backoffice login : redirection vers le template de connexion de l'administration
         * Sinon si route FrontOffice login : redirection vers le template de connexion du Frontoffice (l'original de FOSUserBundle)
         */
        if ($requestAttributes->get('_route') == 'fos_admin_user_security_login') {
            $template = sprintf('BiginfoAdminBundle:Security:login.html.twig');
        } else {
            $template = sprintf('FOSUserBundle:Security:login.html.twig');
        }

        return $this->container->get('templating')->renderResponse($template, $data);
    }

}

Biginfo \ UserBundle \ Resources \ config \ routing.yml

    biginfo_user_user:
    resource: "@BiginfoUserBundle/Resources/config/routing/user.yml"
    prefix:   admin/user

# Route de connexion au FrontOffice
fos_user_security_login:
    pattern: /login
    defaults: { _controller: FOSUserBundle:Security:login }

# Route de vérification de connexion au FrontOffice
fos_user_security_check:
    pattern: /check
    defaults: { _controller: FOSUserBundle:Security:check }
    requirements:
        _method: POST

# Route de déconnexion du FrontOffice
fos_user_security_logout:
    pattern: /logout
    defaults: { _controller: FOSUserBundle:Security:logout }

# Route de connexion au Backoffice
fos_admin_user_security_login:
    pattern:  ad/login
    defaults: { _controller: FOSUserBundle:Security:login }

# Route de vérification de connexion au Backoffice
fos_admin_user_security_check:
    pattern:  ad/check
    defaults: { _controller: FOSUserBundle:Security:check }
    requirements:
        _method: POST

# Route de déconnexion du Backoffice
fos_admin_user_security_logout:
    pattern:  /logout
    defaults: { _controller: FOSUserBundle:Security:logout }

我该如何解决?

1 个答案:

答案 0 :(得分:2)

<强> Security.yml

安全性:     编码器:         Symfony \ Component \ Security \ Core \ User \ User:plaintext         公司\ AngularBundle \ Entity \ User:明文         公司\ AngularBundle \ Entity \ Admin:plaintext

role_hierarchy:
    ROLE_ADMIN:       ROLE_USER
    ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

providers:
   users:
       entity: { class: CompanyAngularBundle:User, property: username }
   admin:
       entity: { class: CompanyAngularBundle:Admin, property: username }

firewalls:
    admin_secured_area:
        pattern:   ^/admin
        anonymous: ~
        provider: admin
        form_login:
            login_path: /admin/login
            check_path: /admin/login_check
            default_target_path: /admin

    user_secured_area:
        pattern:   ^/
        anonymous: ~
        provider: users
        form_login:
            login_path: login
            check_path: login_check
            default_target_path: /home

<强>的routing.yml

login_check:

path: /login_check

admin_login_check:

path: /admin/login_check

Twig文件

登录表单的操作应该是这样的

<form action="{{ path('login_check') }}" method="post">

admin / login表单的操作应该是这样的

<form action="{{ path('admin_login_check') }}" method="post">