簡體   English   中英

用戶注冊后Symfony2自動登錄

[英]Symfony2 auto-login after user registration

我看過這方面的一些帖子,特別是Auto-Login via URL with symfony2 (我無法使用,因為我不知道需要進入 $request 的內容)和Automatic post-registration user authentication 我試過后者,但它沒有登錄。

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 }

來自控制器函數 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);

我正在嘗試登錄到成員防火牆。 我對此知之甚少,無法進行故障排除。 其中大部分只是復制/粘貼/編輯。 任何幫助/解釋將不勝感激。 我也看過Symfony2 auto-login after registration下的文章,但我不明白這有什么意義,因為我不需要用戶跨不同的防火牆登錄,只需要用戶在成員防火牆下登錄。

我注意到的一件事是,用戶實體在訪問數據庫時填充了它的 salt 字段,即使我沒有設置 salt 並且我看不到任何自動設置 salt 的東西。 我還沒有使用鹽,因為我還沒有加密我的密碼(只是嘗試首先使用純文本密碼),並且在嘗試使用創建的用戶登錄時(確實已創建,只是沒有登錄) ) 除非我清除用戶的鹽,否則我無法登錄。 我不知道這是否與自動登錄不起作用有關。

這里的問題在於行$this->get('session')->set('_security_main',serialize($token)); . “security_main”的“_main”也是您進行身份驗證的防火牆。 所以它應該是“_security_members”。 更改后,代碼按原樣工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM