2

我的 security.yml 如下:

jms_security_extra:
    secure_all_services: false
    expressions: true

security:
    encoders:
        Taden\MainBundle\Entity\Employee:   
            algorithm: sha512
            iterations: 2
            encode_as_base64: true

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

    providers:
        main:
            entity: { class: Taden\MainBundle\Entity\Employee, property: code }

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

        login:
            pattern:  ^/login$
            anonymous: ~

        secured_area:
            pattern:    ^/
            form_login: ~
            logout:
                path:   /logout
                target: /login
            http_basic: ~
            anonymous: ~

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

我的 loginAction() 如下:

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

    // get the login error if there is one
    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);
    }

    if ($error != null) 
    {
        $error_message = $this->get('translator')->trans($error->getMessage());
        $this->get('session')->getFlashBag()->add('error', $error_message);            
    }

    if ($this->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY'))
    {
        return $this->redirect($this->generateUrl('menu'));
    }
    else
    {
        $this->get('session')->getFlashBag()->add('error', 'not logged in'); 
    }

    return $this->render('TadenMainBundle:Default:login.html.twig', array(
        'company_name' => '',
        'department_name' => '',
        'user_name' => '',
        'last_username' => $session->get(SecurityContext::LAST_USERNAME),
    ));
}

在本地主机上它可以正常工作。在正确的凭据后再次显示生产服务器登录页面。我检查了数据库连接是否正常工作。问题是 isGranted('IS_AUTHENTICATED_FULLY') 返回 false。如果我使用它没有帮助

<input type="hidden" name="_target_path" value="/menu" />

在 login.html.twig 中。有人可以指出我解决问题的正确方向吗?

4

2 回答 2

4

在 Symfony 2 中,当您的用户拥有与其自身相关联的角色(至少一个)时,您将获得完全身份验证。

尝试为您的用户添加一个角色,您应该在调试栏中看到您已使用您提供给用户的角色进行了完全身份验证(然后“IS_AUTHENTICATED_FULLY”应该为真)。

于 2013-05-22T09:34:13.610 回答
1

Taden\​​MainBundle\Entity\Employee 类必须实现 EquatableInterface

该方法应该是这样的

public function isEqualTo(UserInterface $user)
{
    return $this->id === $user->getId();
}
于 2016-03-02T00:36:39.137 回答