symfony - 手动验证用户

标签 symfony

我尝试验证用户身份:

<?php 

/**
 * @Route("/testLogin", name="testLogin")
 */
public function testLoginAction()
{
    $em = $this->getDoctrine()->getEntityManager();
    $user = $em->getRepository('ApplicationDefaultBundle:User')->findOneBy(array('id' => 126));

    $providerKey = 'main';
    $token = new UsernamePasswordToken($user, null, $providerKey, $user->getRoles());

    $this->container->get('security.context')->setToken($token);

    return $this->redirect($this->generateUrl('testCheck'));
}

/**
 * @Route("/testCheck", name="testCheck")
 */
public function testCheckAction()
{
    if (false === $this->get('security.context')->isGranted(
        'IS_AUTHENTICATED_REMEMBERED'
    )) {
        return new Response('Not logged');
    }
    $user = $this->container->get('security.context')->getToken()->getUser();

    return new Response($user->getUsername.' is logged');
}

但我得到永久 302 重定向到/login 页面。

security:
    encoders:
        Application\Bundle\DefaultBundle\Entity\User:
            algorithm:   sha512
            iterations: 24
            encode_as_base64: true

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

    providers:
        main:
            entity: { class: Application\Bundle\DefaultBundle\Entity\User, property: username }

    firewalls:   
        secured_area:
            remember_me:
                key:      MySecretKeyBlablabla
                lifetime: 36000000
                path:     /
                domain:   ~
            pattern:    ^/
            form_login:
                check_path: /login_check
                login_path: /login
                provider: main
            logout:
                path:   /logout
                target: /
            anonymous: true

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

我从 https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Controller/RegistrationController.php 获取的验证代码

app/logs/dev.log 中出现错误:

redirecting to authentication entry point (No Authentication Provider found for token of class "Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken".) [] []

在清理 cookie 之前我无法访问网站。

最佳答案

尝试来自 this answer 的代码。在您的情况下,防火墙名称是 secured_area:

// your controller action
public function myAction()
{

    // Authenticating user
    $token = new UsernamePasswordToken($user, null, 'secured_area', $user->getRoles());
    $this->get('security.token_storage')->setToken($token);
    //For Symfony <= 2.3
    //$this->get('security.context')->setToken($token);
    $this->get('session')->set('_security_secured_area', serialize($token));

}

关于symfony - 手动验证用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8637172/

相关文章:

symfony - NelmioApiDocBundle 不起作用 "No operations defined in spec!"

php - 创建密码恢复表-插入值时出错

symfony - 如何在 Symfony Twig 渲染中检测小部件类型?

php - 如何修复 : "Sensio\Bundle\FrameworkExtraBundle\Configuration\Route" is deprecated

带注释的 Symfony2 路由

php - Symfony Doctrine 存储库 (findOneBy) 结果不是对象

php - 将 Zend Search Lucene 与 Symfony 2 结合使用

symfony - 我想在 WebTestCase 中集成 getContainer()

php - 正确的单元测试

php - Symfony2更新 “subform”中的项目