How to differ sessions in browser-tabs?

后端 未结 21 1359
情深已故
情深已故 2020-11-22 08:48

In a web-application implemented in java using JSP and Servlets; if I store information in the user session, this information is shared from all the tabs from the same brows

相关标签:
21条回答
  • 2020-11-22 08:57

    You can use HTML5 SessionStorage (window.sessionStorage). You will generate a random id and save in session Storage per Browser Tab. Then each browser tab has his own Id.

    Data stored using sessionStorage do not persist across browser tabs, even if two tabs both contain webpages from the same domain origin. In other words, data inside sessionStorage is confined to not just the domain and directory of the invoking page, but the browser tab in which the page is contained in. Contrast that to session cookies, which do persist data from tab to tab.

    0 讨论(0)
  • 2020-11-22 08:57

    Another approach that works is to create a unique window id and store this value along with the session id in a database table. The window id I often use is integer(now). This value is created when a window is opened and re-assigned to the same window if the window is refreshed, reloaded or submitted to itself. Window values (inputs) are saved in the local table using the link. When a value is required, it is obtained from the database table based on the window id / session id link. While this approach requires a local database, it is virtually foolproof. The use of a database table was easy for me, but I see no reason why local arrays would not work just as well.

    0 讨论(0)
  • 2020-11-22 08:57

    I resolved this of following way:

    • I've assigned a name to window this name is the same of connection resource.
    • plus 1 to rid stored in cookie for attach connection.
    • I've created a function to capture all xmloutput response and assign sid and rid to cookie in json format. I do this for each window.name.

    here the code:

    var deferred = $q.defer(),
            self = this,
            onConnect = function(status){
              if (status === Strophe.Status.CONNECTING) {
                deferred.notify({status: 'connecting'});
              } else if (status === Strophe.Status.CONNFAIL) {
                self.connected = false;
                deferred.notify({status: 'fail'});
              } else if (status === Strophe.Status.DISCONNECTING) {
                deferred.notify({status: 'disconnecting'});
              } else if (status === Strophe.Status.DISCONNECTED) {
                self.connected = false;
                deferred.notify({status: 'disconnected'});
              } else if (status === Strophe.Status.CONNECTED) {
                self.connection.send($pres().tree());
                self.connected = true;
                deferred.resolve({status: 'connected'});
              } else if (status === Strophe.Status.ATTACHED) {
                deferred.resolve({status: 'attached'});
                self.connected = true;
              }
            },
            output = function(data){
              if (self.connected){
                var rid = $(data).attr('rid'),
                    sid = $(data).attr('sid'),
                    storage = {};
    
                if (localStorageService.cookie.get('day_bind')){
                  storage = localStorageService.cookie.get('day_bind');
                }else{
                  storage = {};
                }
                storage[$window.name] = sid + '-' + rid;
                localStorageService.cookie.set('day_bind', angular.toJson(storage));
              }
            };
        if ($window.name){
          var storage = localStorageService.cookie.get('day_bind'),
              value = storage[$window.name].split('-')
              sid = value[0],
              rid = value[1];
          self.connection = new Strophe.Connection(BoshService);
          self.connection.xmlOutput = output;
          self.connection.attach('bosh@' + BoshDomain + '/' + $window.name, sid, parseInt(rid, 10) + 1, onConnect);
        }else{
          $window.name = 'web_' + (new Date()).getTime();
          self.connection = new Strophe.Connection(BoshService);
          self.connection.xmlOutput = output;
          self.connection.connect('bosh@' + BoshDomain + '/' + $window.name, '123456', onConnect);
        }
    

    I hope help you

    0 讨论(0)
  • 2020-11-22 08:59

    I see many implementations which have client side changes to manipulate session id cookies. But in general session id cookies should be HttpOnly so java-script cannot access otherwise it may lead to Session Hijack thru XSS

    0 讨论(0)
  • 2020-11-22 09:00

    The window.name Javascript property, is the only thing that will persist across tab activity, but can remain independent (instead of URL guff).

    0 讨论(0)
  • 2020-11-22 09:03

    You have to realize that server-side sessions are an artificial add-on to HTTP. Since HTTP is stateless, the server needs to somehow recognize that a request belongs to a particular user it knows and has a session for. There are 2 ways to do this:

    • Cookies. The cleaner and more popular method, but it means that all browser tabs and windows by one user share the session - IMO this is in fact desirable, and I would be very annoyed at a site that made me login for each new tab, since I use tabs very intensively
    • URL rewriting. Any URL on the site has a session ID appended to it. This is more work (you have to do something everywhere you have a site-internal link), but makes it possible to have separate sessions in different tabs, though tabs opened through link will still share the session. It also means the user always has to log in when he comes to your site.

    What are you trying to do anyway? Why would you want tabs to have separate sessions? Maybe there's a way to achieve your goal without using sessions at all?

    Edit: For testing, other solutions can be found (such as running several browser instances on separate VMs). If one user needs to act in different roles at the same time, then the "role" concept should be handled in the app so that one login can have several roles. You'll have to decide whether this, using URL rewriting, or just living with the current situation is more acceptable, because it's simply not possible to handle browser tabs separately with cookie-based sessions.

    0 讨论(0)
提交回复
热议问题