簡體   English   中英

Symfony-重定向后身份驗證丟失

[英]Symfony - Authentication lost after redirection

我的Symfony項目遇到問題。

我有兩個Symfony應用程序(分別稱為A和B),它們可以通過Web Service相互通信。 在其中一個(應用程序A)中,我存儲了我的用戶列表,並嘗試通過應用程序B對用戶進行身份驗證。預期的行為是:-用戶輸入其用戶名和密碼-應用程序B獲取了用戶名/密碼-應用程序B通過WS調用應用程序A,詢問此用戶是否正常-如果用戶確定,則應用程序B創建會話令牌並驗證該用戶。

目前,我可以對用戶進行身份驗證,但是每次瀏覽應用B時,我都會丟失會話令牌(因此,我將重定向到/ login頁面)。 奇怪的是,我仍在通過身份驗證,但是會話令牌不再包含我的User對象(它包含一個帶有每個屬性“ null”的User對象)。

這是我的security.yml:

providers:
    webservice:
        id: webservice_user_provider

firewalls:
    login:
        pattern: ^/login$|^/check$|^/_wdt
        anonymous: true
    secured:
        pattern: ^/
        anonymous: false
        form_login:
            check_path: /login_check
            login_path: login
            username_parameter:  username
            password_parameter:  password
            default_target_path: /
        logout:
            path:   logout

access_control:

我的LoginController(應用B):公共功能checkAction(Request $ request){
//如果需要($ request-> getMethod()==“ POST”){$ username = $ request-> get(“ username”); $ password = $ request-> get(“ password”);

        // Interrogation du repo pour savoir si l'utilisateur existe
        $repo = $this->getDoctrine()->getRepository("EntrepotEntitiesBundle:Utilisateurecommercant");

        /* @var $repo \Entrepot\EntitiesBundle\Repository\UtilisateurecommercantRepository */
        $user = $repo->findByLoginAndPassword($username, $password);

        if ($user != null) // On a retrouvé un utilisateur => OK
        {
            // On sérialise alors un token de connexion dans la session
            $this->login($request, $user);
            return $this->indexAction();
        }
    }

    return $this->render('EntrepotUtilisateurBundle::index.html.twig', array(
        'last_username' => $this->getRequest()->getSession()->get(SecurityContext::LAST_USERNAME),
    ));
}


public function login(Request $request, UserInterface $user)
{
    $token = new UsernamePasswordToken($user, $user->getPassword(), 'secured', $user->getRoles());
    $request->getSession()->set('_security_secured', serialize($token));
    $this->get("security.context")->setToken($token);

    // Et on lève un évènement "login"
    $event = new InteractiveLoginEvent($request, $token);
    $this->get("event_dispatcher")->dispatch("security.interactive_login", $event);
}

我不明白的一件事是何時以及如何checkAction() 目前,僅當我的用戶驗證其登錄表單時才調用它。 每次嘗試導航到新頁面時都應該調用它嗎? 我不太了解它是如何工作的,我想更好地了解它。

我忘記了什么嗎?

謝謝。

您必須為form_login設置提供者密鑰,即:

form_login:
    ...
    provider: webservice

我已經可以使用此鏈接解決我的問題:

http://blog.vandenbrand.org/2012/06/19/symfony2-authentication-provider-authenticate-against-webservice/

講得很好,我很好理解,現在我可以通過WS對用戶進行身份驗證了:)

暫無
暫無

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

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