简体   繁体   English

用户注册后Symfony2自动登录

[英]Symfony2 auto-login after user registration

I have seen some posts in this regard, specifically Auto-Login via URL with symfony2 (which I can't use because I do not know what needs to go into $request) and Automatic post-registration user authentication .我看过这方面的一些帖子,特别是Auto-Login via URL with symfony2 (我无法使用,因为我不知道需要进入 $request 的内容)和Automatic post-registration user authentication I tried the latter, but it is not logging in.我试过后者,但它没有登录。

Code from security.yml security.yml 中的代码

 firewalls:
    admin_login_firewall:
      pattern:    ^/admin/login$
      anonymous:  ~
    admin:
        pattern:    ^/admin
        form_login:
            login_path:  admin_login
            check_path:  admin_login_check
            default_target_path: admin_dashboard  
        logout:
            path:   admin_logout
            target: admin_login
        http_basic:
            realm: "Licensing Admin Portal"
    member_login_firewall:
      pattern:    ^/members/login$
      anonymous:  ~
    members:
        pattern:    ^/members
        form_login:
            login_path:  member_login
            check_path:  member_login_check
            default_target_path: member_dashboard  
        logout:
            path:   member_logout
            target: home
        http_basic:
            realm: "Licensing Member Portal"        


encoders:
    Pmb\LicensingBundle\Entity\User: plaintext

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

Code snippet from Controller function saveUserAction():来自控制器函数 saveUserAction() 的代码片段:

    ... // Code preceding this point creates user entity and sets all fields
    $em = $this->getDoctrine()->getManager();
    if (empty($data['user_id'])) $em->persist($user);
    $em->flush();

    if (!empty($organization)) $this->linkOrganizationUserAction($organization,$user, true);

    if (isset($data['registering']))
    {
        $token = new UsernamePasswordToken($user, null, 'members', $user->getRoles());
        $this->get('security.context')->setToken($token);
        $this->get('session')->set('_security_main',serialize($token));
    }

    return $this->createJsonResponse($user);

I am trying to log in to the members firewall.我正在尝试登录到成员防火墙。 I do not know enough about this to troubleshoot.我对此知之甚少,无法进行故障排除。 Most of this is just copy/paste/edit.其中大部分只是复制/粘贴/编辑。 Any help / explanation would be greatly appreciated.任何帮助/解释将不胜感激。 I also read the article under Symfony2 auto-login after registration , but I do not see the significance of this, as I do not need have users logged in accross different firewalls, and just need the user logged in under the members firewall.我也看过Symfony2 auto-login after registration下的文章,但我不明白这有什么意义,因为我不需要用户跨不同的防火墙登录,只需要用户在成员防火墙下登录。

One thing that I DID notice, is that the user entity is having its salt field populated when persiting to the database, even though I did not set a salt and I cannot see anything auto-setting the salt.我注意到的一件事是,用户实体在访问数据库时填充了它的 salt 字段,即使我没有设置 salt 并且我看不到任何自动设置 salt 的东西。 I am not yet using the salt as I am not yet encrypting my passwords (just trying to get it working with plain text passwords first), and when trying to log in with a created user (which does get created, just not getting logged in) I cannot log in unless I clear the salt on the user.我还没有使用盐,因为我还没有加密我的密码(只是尝试首先使用纯文本密码),并且在尝试使用创建的用户登录时(确实已创建,只是没有登录) ) 除非我清除用户的盐,否则我无法登录。 I don't know if this has anything to do with the fact that the auto-login is not working.我不知道这是否与自动登录不起作用有关。

The issue here was with the line $this->get('session')->set('_security_main',serialize($token));这里的问题在于行$this->get('session')->set('_security_main',serialize($token)); . . The "_main" of "security_main" is also the firewall that you are authenticating against. “security_main”的“_main”也是您进行身份验证的防火墙。 So it should have been "_security_members".所以它应该是“_security_members”。 After changing it the code worked as is.更改后,代码按原样工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM