English 中文(简体)
在 yymfony 2, 登录动作显示 当我实际登录时, 我被登入了 。
原标题:In symfony2, the login action is showing me as logged out when I m actually logged in

如果登录用户去登录操作, 我想将它们重定向到另一页。 但我无法找到如何检测用户是否在 login Action 方法内登录。 登录操作中的安全环境使我不登录时我似乎已经登录了 。

作为测试,我在登录网站时请求提供以下两个页面。 为什么我无法在登录操作中访问用户?

以下是我的登录操作 :

public function loginAction()
{
    $token = $this->get( security.context )->getToken();
    print_r(get_class($token));
        // Outputs "SymfonyComponentSecurityCoreAuthenticationTokenAnonymousToken"
    print_r($token->getUser());
        // Outputs "anon."
}

以下是申请中的一般性行动,受登录保护:

public function regularAction()
{
    $token = $this->get( security.context )->getToken();
    print_r(get_class($token));
        // Outputs "SymfonyComponentSecurityCoreAuthenticationTokenUsernamePasswordToken"
    print_r(get_class($token->getUser()));
        // Outputs "CompanyBaseBundleEntityUser"
}

这是我的security.yml :

security:
    encoders:
        CompanyBaseBundleEntityUser:
            algorithm:   sha1
            iterations: 1
            encode_as_base64: false
    providers:
        main:
            entity: { class: CompanyBaseBundleEntityUser, property: user_name }
    firewalls:
        login_firewall:
            pattern:    ^/login$
            anonymous:  ~
        main:
            pattern: ^/
            form_login:
                login_path: /login
                check_path: /login_check
                post_only: true
                always_use_default_target_path: false
                default_target_path: /
                use_referer: true
                failure_path: null
                failure_forward: false
                username_parameter: user_name
                password_parameter: password_hash
                csrf_parameter: _csrf_token
                intention: authenticate
            logout:
                path: /logout
                target: /
    acl:
        connection: default

<强力>EDIT:我不认为我的其余防火墙是相关的, 但在阅读 ilanco 回答后,

security:
    encoders:
        CompanyBaseBundleEntityUser:
            algorithm:   sha1
            iterations: 1
            encode_as_base64: false

    providers:
        main:
            entity: { class: CompanyBaseBundleEntityUser, property: user_name }

    firewalls:
        dev:
            pattern:  ^/(_(profiler|wdt)|css|images|js)/
            security: false

        login_firewall:
            pattern:    ^/login$
            anonymous:  ~
        password_reset:
            pattern:    ^/passwordreset/*$
            anonymous:  ~
        error_firewall:
            pattern:    ^/error/.*$
            anonymous:  ~
        unsupported_broswers:
            pattern:    ^/unsupported$
            anonymous:  ~
        security_question_firewall:
            pattern:  ^/user/(locked|security_question)/(new)*$
            anonymous:  ~
        api_firewall:
            pattern:  ^/api/.*$
            provider: main
            http_basic:
                realm: "Secured API Area. Login with your regular credentials"
                provider: main
        main:
            pattern: ^/
            form_login:
                login_path: /login
                check_path: /login_check
                post_only: true
                always_use_default_target_path: false
                default_target_path: /
                use_referer: true
                failure_path: null
                failure_forward: false
                username_parameter: user_name
                password_parameter: password_hash
                csrf_parameter: _csrf_token
                intention: authenticate
            logout:
                path: /logout
                target: /
    acl:
        connection: default

根据Ilanco的建议,我删除了以下内容:

    login_firewall:
        pattern:    ^/login$
        anonymous:  ~

并在 提供者 一节中直接添加以下内容:

access_control:
    - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }

但是当我访问/登录时,我有一个重定向回路错误。

最佳回答

我也为这个问题苦苦挣扎

/login 不是主防火墙的一部分, 因为用户无法进入那里 。

解决这个问题的方法是删除您称之为 login_firewalk 的自定义防火墙,并允许通过 ACL 访问 /login

在您的 security.yml 中添加以下代码

access_control:
    - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
问题回答

被管理解决了这个问题 - 重定向环的问题是由无法访问 / login 页面造成的。 我仅做了一个防火墙, 设置匿名访问: ~, 定义了非用户和voila 的存取控制!

security:
    firewalls:
        dev:
            pattern:  ^/(_(profiler|wdt)|css|images|js)/
            security: true
            anonymous: ~ 
        secured_area:
            pattern:    ^/
            anonymous:  ~
            form_login:
                login_path:  /login
                check_path:  /login_check
                always_use_default_target_path: true
                default_target_path: /
            logout:
                path:   /logout
                target: /
    providers:
        main:
            entity: { class: CoreUserBundleEntityUser, property: username }
    encoders:
        CoreUserBundleEntityUser: 
            algorithm:   sha256
            iterations: 10
            encode_as_base64: true
    access_control:
        - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin, roles: ROLE_SUPERADMIN }
        - { path: ^/user, roles: ROLE_USER }
        - { path: ^/, roles: IS_AUTHENTICATED_FULLY }




相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Remotely authenticating client Windows user on demand

Suppose I am writing a server for a particular network protocol. If I know that the client is running on a Windows machine, is it possible for my server to authenticate the Windows user that owns the ...

Role/Permission based forms authorizing/authentication?

While looking into forms authorizing/authentication, I found that it is possible to do role based authorizing by adding an array of roles to a FormsAuthenticationTicket. That way I can write User....

热门标签