登录后Symfony2说:已验证:否

时间:2015-10-29 05:22:55

标签: symfony authentication

经过身份验证:成功登录后,Symfony2开发工具栏中不会显示

在我的成功处理程序中,我可以访问$token->getRoles()并查看分配给用户的角色对象,因此它似乎正在序列化。

所以我不确定为什么它没有进行身份验证。

这是我的security.yml:

security:
encoders:
    FixedApp\Model\User:
      algorithm:        sha1
      encode_as_base64: false
      iterations:       1

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

providers:
    administrators:
        entity: { class: FixedApp\Model\User, property: username }

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

    login:
        pattern:  ^/$
        security: false

    secured_area:
        pattern: ^/
        form_login:
            check_path: fixed_app_authentication_login
            login_path: fixed_app_homepage
            username_parameter: form[username]
            password_parameter: form[password]
            default_target_path: fixed_app_hub_homepage
            always_use_default_target_path: true
            success_handler: security.authentication.success_handler
        logout:
            path: fixed_app_authentication_logout
            target: fixed_app_homepage

access_control:
- { path: ^/log-in$, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: /users/edit, roles: ROLE_ADMIN }

不进行身份验证是一个问题,因为当我作为管理员转到/users/edit时,它会拒绝访问。所以我需要弄清楚这里发生了什么。任何想法都会受到最高的赞赏。

1 个答案:

答案 0 :(得分:1)

我在网上看到其他一些人遇到同样的问题,但我以前从未见过任何解决方案 - 所以希望这对某人有所帮助。

UserRole.php课程中,我错过了这个功能:

/**
 * @see RoleInterface
 */
public function getRole()
{
    return $this->role;
}

其次,在User.php课程中,我实现了EquatableInterface

use Symfony\Component\Security\Core\User\EquatableInterface;
use Symfony\Component\Security\Core\User\UserInterface;

...

class User implements AdvancedUserInterface, EquatableInterface, \Serializable
{
    ...

    public function isEqualTo(UserInterface $user)
    {
        if ($this->getId() == $user->getId())
        {
            return true;
        }

        else
        {
            return false;
        }
    }

然后它开始工作了。 Symfony工具栏按钮变为绿色,表示已验证:是,它列出了该用户的所有角色。

相关问题