简体   繁体   English

如何从Symfony2应用程序获取第三方应用程序会话数据?

[英]How to get third-party app session data from a Symfony2 app?

In order to implement cross application authentication (getting logged in in my Symfony2 app if the user already logged in in an other application), I made a Symfony2 listener class that checks if specific data concerning the user is in the session. 为了实现跨应用程序身份验证(如果用户已经登录其他应用程序,则在我的Symfony2应用程序中登录),我制作了一个Symfony2侦听器类,该类检查与该用户有关的特定数据是否在会话中。 This data comes from a non-Symfony2 (but PHP) app. 此数据来自非Symfony2(但PHP)应用程序。

The problem is that the session data from the other app is not present in the session object I use in my class. 问题是我在课堂上使用的会话对象中没有来自其他应用程序的会话数据。

Here is the listener (simplified) class: 这是侦听器(简化)类:

<?php
class OldAppAuthenticationListener
{
    /** @var \Symfony\Component\Security\Core\SecurityContext */
    private $context;

    public function __construct(SecurityContext $context)
    {
        $this->context = $context;
    }

    public function onKernelRequest(GetResponseEvent $event)
    {
        if (HttpKernel::MASTER_REQUEST != $event->getRequestType()) {
            // don't do anything if it's not the master request
            return;
        }

        if (!$this->context->isGranted('IS_AUTHENTICATED_FULLY')) {
            $request = $event->getRequest();
            $session = $request->getSession();

            $userName = $session->get('nomUtilisateur');

            $token = new PreAuthenticatedToken($userName, null, 'secured_area', array('ROLE_USER'));

            $session->set('_security_secured_area',  serialize($token));
        }
    }
}

It is registered in services.yml like this: 像这样在services.yml中注册:

services:
    my_app.listener.old_app_authentication:
        class: Acme\MyAppBundle\Listener\MyAppAuthenticationListener
        arguments: ["@security.context"]
        tags:
            - { name: kernel.event_listener, event: kernel.request }

But the $session->get('nomUtilisateur') always returns NULL (and $session->all() and $_SESSION only return some Symfony2 specific vars) although the other app stores all this data in the session. 但是$session->get('nomUtilisateur')始终返回NULL(而$session->all()$_SESSION仅返回某些Symfony2特定的变量),尽管另一个应用程序将所有这些数据存储在会话中。

Of course, I use the same cookie session domain for both apps (as configured in config.yml) and I can easily check that the PHPSESSID is the same. 当然,我为两个应用程序使用相同的cookie会话域(在config.yml中配置),并且我可以轻松地检查PHPSESSID是否相同。

So here is my question: why are the old app session variables not available and how can I get them from my listener class? 所以这是我的问题:为什么旧的应用程序会话变量不可用?如何从侦听器类中获取它们?

As stated here , Symfony2 uses session bags to store session stuff. 如前所述这里 ,Symfony2中使用会话袋来存储会话的东西。 This means that you have to directly access the $_SESSION superglobal for such a functionality. 这意味着您必须直接访问$_SESSION超全局变量才能使用此功能。

For me the solution was to use directly the php session fonctions. 对我来说,解决方案是直接使用php会话功能。 also I had to check if session name, domain and save path are the same on both applications. 我还必须检查两个应用程序上的会话名称,域和保存路径是否相同。

In my symfony I had to add: 在我的symfony中,我必须添加:
session_save_path('c:/wamp/tmp'); session_save_path('c:/ wamp / tmp');
session_name(_SESSION_ID_); session_name(_SESSION_ID_);
session_start(); session_start();
and then use $_SESSION 然后使用$ _SESSION

an other way that was given to me, but I didn't use is to use the sessions files directly like this: 提供给我的另一种方法,但是我没有使用的方法是像这样直接使用会话文件:

"The trick was using the session cookie sent by the browser. “诀窍是使用浏览器发送的会话cookie。

For example, an old web application, written in PHP, sent a cookie called IntranetSession to the browser. 例如,一个用PHP编写的旧Web应用程序向浏览器发送了一个名为IntranetSession的cookie。 As I knew where does PHP store the session files, I simply opened that file, and decoded its contents with session_decode(). 据我所知,PHP将会话文件存储在哪里,我只是打开了该文件,并使用session_decode()对其内容进行了解码。 The drawbacks of this is that session_decode() puts its output directly into $_SESSION, overwriting you current session data (even the stuff put there by Symfony). 这样做的缺点是session_decode()会将其输出直接放入$ _SESSION中,从而覆盖您当前的会话数据(甚至包括Symfony放置的内容)。 Basically the rule is as follows: 基本上,规则如下:

$sessionData = file_get_contents($sessionFile);<br>
$tmpSess = $_SESSION;<br>
session_decode($sessionData);<br>
$otherAppSession = $_SESSION;<br>
$_SESSION = $tmpSess;<br>



"

hope this helps! 希望这可以帮助!

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

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