配置身份验证symfony2

时间:2014-03-02 22:26:51

标签: php symfony

我正在尝试使用此配置在symfony2上配置身份验证:

Security.yml

security:
    encoders:
        Symfony\Component\Security\Core\User\User: plaintext

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

    providers:
        in_memory:
            memory:
                users:
                    user:  { password: userpass, roles: [ 'ROLE_USER' ] }
                    admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] }

    firewalls:
        admin_area:
            pattern:   ^/admin
            provider:  in_memory
            anonymous: ~
            form_login:
                login_path: login
                check_path: login_check
            logout:
                path:   /logout
                target: /  

    access_control:
        - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin, roles: ROLE_ADMIN }

/src/MyBundle/Resources/Routing.yml

ies_cierva_encuesta_backend_admin:
    pattern:  /admin
    defaults: { _controller: Bundle:Default:admin }

login:
    pattern:  /login
    defaults: { _controller: Bundle:Login:login }

login_check:
    pattern:  /login_check  

logout:
    pattern:  /logout

的src /捆绑/控制器/ LoginController.php

<?php

namespace ...

use ...

class LoginController extends Controller {

    public function loginAction(Request $request) {
        $session = $request->getSession();

        if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
            $error = $request->attributes->get(
                SecurityContext::AUTHENTICATION_ERROR
            );
        } else {
            $error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
            $session->remove(SecurityContext::AUTHENTICATION_ERROR);
        }

        return $this->render(
            'Bundle:Security:login.html.twig',
            array(
                // last username entered by the user
                'last_username' => $session->get(SecurityContext::LAST_USERNAME),
                'error'         => $error,
            )
        );
    }
}

我收到了这个错误:

“无法找到路径控制器”/ login_check“。也许您忘记在路由配置中添加匹配路由了?”

如果我没错,这条路线不需要控制器......

1 个答案:

答案 0 :(得分:1)

http://symfony.com/doc/current/book/security.html中,提到了

“确保您的check_path网址(例如/ login_check)位于您用于表单登录的防火墙后面。”

但/ login_check不在您用于表单登录的同一防火墙后面。

firewalls:
    admin_area:
        pattern:   ^/admin
        provider:  in_memory
        anonymous: ~
        form_login:
            login_path: login
            check_path: login_check
        logout:
            path:   /logout
            target: /  

在上面的配置中,模式路径“login_check”与“^ / admin”模式不匹配。相应地更改模式以使其工作。