Tomcat (Jboss-web) enables all level two domain names to be shared SessionID

Source: Internet
Author: User
Tags jboss

I. Demand.

Our company wants to collect data from each website, want to know a user's entire browsing track on our website, but each of our two level domain name corresponds to own project, that is correspondence own jsessionid, we cannot associate these data together, So think of all the two-level domain name to be a common jsessionid, so that the user's entire browsing data we can get.


Two. Development.

You need to modify the source of Tomcat (Jboss-web), there are two places to modify.

The Configuresessioncookie method of 1.org.apache.catalina.connector.request

 /**     * configures the given jsessionid cookie.      *     *  @param  cookie The  jsessionid cookie to be configured     */     Protected void configuresessioncookie (Tomcatcookie cookie)  {         cookie.setmaxage ( -1);        if  ( Context.getsessioncookie (). GetPath ()  != null)  {             cookie.setpath (Context.getsessioncookie (). GetPath ());         } else {             String contextpath = context.getencodedpath ();             if  ("". Equals (cOntextpath))  {                 contextPath =  "/";             }             cookie.setpath (ContextPath);         }        if  ( Context.getsessioncookie (). Getcomment ()  != null)  {             cookie.setcomment (Context.getsessioncookie (). Getcomment ());         }        if  ( Context.getsessioncookie (). GetDomain ()  != null)  {             cookie.setdomain (Context.getsessioncookie (). GetDomain ());         }        if  (Context.getsessioncookie (). Ishttponly ())  {             cookie.sethttponly (true);         }                 cookie.setpath ("/");         cookie.setdomain (System.getproperty ("CookieDomain"));                  if  (Context.getsessioncookie (). Issecure ())  {             cookie.setsecure (True);        } else if  ( Issecure ())  {            cookie.setsecure ( true);         }    }

The added code is

Cookie.setpath ("/"); Cookie.setdomain (System.getproperty ("Cookiedomain"));

2. Modify the catalina.properties under Conf in the compiled Tomcat

Add Cookiedomain variable cookiedomain=.cdeledu.com


Three. Principle

How Tomcat reads the client's Jsessionid

During the request to Tomcat, Need to access the Org.apache.catalina.connector.CoyoteAdapter Sevice method, which calls the Postparserequest method, which is the main parsing client Jsessionid, where Parsesessioni D mainly analyze the jessionid,parsesessioncookiesid in the URL. This is the jsessionid in the analysis cookie.

The code is as follows:

Get Jsesionid from URL

    /**     * parse session id in url.      */    protected void parsesessionid ( Org.apache.coyote.request req, request request)  {         bytechunk uribc = req.requesturi (). Getbytechunk ();      The value of    //math is: "; jsessionid=" &NBSP;, determine if the URL has this string          int semicolon = uribc.indexof (Match, 0, match.length (),  0);         //If there is parsing and assigning a value to Request (org.apache.catalina.connector.Request)          if  (semicolon > 0)  {             // parse session id, and extract it  from the decoded request  uri            int start =  Uribc.getstart ();             int end =  uribc.getend ();            int  Sessionidstart = semicolon + match.length ();             int semicolon2 = uribc.indexof ('; ',  sessionIdStart);             if  (semicolon2 >= 0)   {                 request.setrequestedsessionid                      (New string (Uribc.getbuffer (), start +  sessionidstart,                              semicolon2 - sessionidstart));                 // Extract session ID from  request uri                 byte[] buf = uribc.getbuffer ();                 for  (int i = 0; i < end -  start - semicolon2; i++)  {                     buf[start + semicolon +  i]                          = buf[start + i + semicolon2];                 }                 uribc.setbytes (buf, start, end - start -  semicolon2 + semicolon);             } else {                 request.setRequestedSessionId                      (New string (Uribc.getbuffer (),  start +  sessionIdStart,                               (end -  Start)  - sessionidstart); &nBsp;               uribc.setend ( Start + semicolon);            }             request.setrequestedsessionurl (True);         } else {             request.setrequestedsessionid (NULL);             request.setrequestedsessionurl (False);         }    }

Get from a cookie

    /**     * parse session id in url.      */    protected void parsesessioncookiesid ( Org.apache.coyote.request req, request request)  {         // Parse session id from cookies  get the cookie    in request      cookies servercookies = req.getcookies ();         int count = servercookies.getcookiecount ();         if  (count <= 0)              return;        for  (int i =  0; i < count; i++)  {             servercookie scoOkie = servercookies.getcookie (i);             //that there is a name Jsessionid in the cookie,             // Assign a value to Request (org.apache.catalina.connector.Request)              if  (Scookie.getname (). Equals (Globals.session_cookie_name))  {                 // override anything  requested in the URL                 if  (!request.isrequestedsessionidfromcookie ())  {                     //  accept only the first session id cookie            &nbsP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CONVERTMB (Scookie.getvalue ());                      request.setrequestedsessionid                          (Scookie.getvalue (). toString ());                      Request.setrequestedsessioncookie (True);                     request.setrequestedsessionurl (False);                     if  (log.isdebugenabled ())                       &Nbsp;  log.debug (" Requested cookie session id is "  +                              request.getrequestedsessionid ());                 } else {                     if  (!request.isrequestedsessionidvalid ())  {                         // replace the  session id until one is valid         &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CONVERTMB ( Scookie.getvalue ());                         request.setRequestedSessionId                               (Scookie.getvalue (). toString ());                     }                 }            }         }    }

Since we are writing to the master domain, we can get this cookie in Tomcat (the range of cookies available in Tomcat is under your own domain and the primary domain).



This article is from the "Stud Programmer's Counter attack" blog, make sure to keep this source http://cdelliqi.blog.51cto.com/9028667/1537278

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.