Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6250933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:31:11+00:00 2026-05-24T13:31:11+00:00

I’m trying to enable SSO under Tomcat such that users who go to http://mydomain.com

  • 0

I’m trying to enable SSO under Tomcat such that users who go to http://mydomain.com and http://www.mydomain.com will have their session cookie available for requests made to http://subdomain.mydomain.com. All three of these domains go to the same webapp, so ideally I’d like to not mess with SSO at all and just set the domain on the standard JSESSIONID cookie.

However, that doesn’t seem possible, so I’m trying to enable Tomcat’s SSO Valve. The problem is that the Valve requires a definition of a Realm, and a Realm is supposed to specify a database of users and roles. However, I am not using container-based authentication nor role-based authorization, so I do not need or want to configure a Realm. All I want is for the session cookie(s) to be able to be shared across each of these different subdomains.

Is there any straightforward way to do this?

Edit

My current workaround for this is to have the server redirect every incoming request to the “canonical” server name. This works well enough, but obviously it is not actually solving the problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T13:31:13+00:00Added an answer on May 24, 2026 at 1:31 pm

    We were having the same problem and created a Tomcat Valve that would overwrite or set the Domain part of the session Cookie. Quite a simple thing and it already works for many years. The code goes like this:

    public class CrossSubdomainSessionValve extends ValveBase {
      public CrossSubdomainSessionValve() {
        super();
        info = "common-tomcat-CrossSubdomainSessionValve";
      }
    
      @Override
      public void invoke(Request request, Response response) throws IOException, ServletException {
        // cookie will only need to be changed, if this session is created by this request.
        if (request.getSession(true).isNew()) {
          Cookie sessionCookie = findSessionCookie(response.getCookies());
          if (sessionCookie != null) {
            String cookieDomainToSet = getCookieDomainToSet(request.getServerName());
            if (cookieDomainToSet != null) {
              // changing the cookie only does not help, because tomcat immediately sets
              // a string representation of this cookie as MimeHeader, thus we also
              // have to change this representation
              replaceCookie(response.getCoyoteResponse().getMimeHeaders(), sessionCookie, cookieDomainToSet);
            }
          }
        }
    
        // process the next valve
        getNext().invoke(request, response);
      }
    
      protected Cookie findSessionCookie(Cookie[] cookies) {
        if (cookies != null)
          for (Cookie cookie : cookies)
            if (Globals.SESSION_COOKIE_NAME.equals(cookie.getName())) {
              return cookie;
        return null;
      }
    
      protected void replaceCookie(MimeHeaders headers, Cookie originalCookie, String domainToSet) {
        // if the response has already been committed, our replacementstrategy will have no effect
    
        // find the Set-Cookie header for the existing cookie and replace its value with new cookie
        for (int i = 0, size = headers.size(); i < size; i++) {
          if (headers.getName(i).equals("Set-Cookie")) {
            MessageBytes value = headers.getValue(i);
            if (value.indexOf(originalCookie.getName()) >= 0) {
              if (originalCookie.getDomain() == null) {
                StringBuilder builder = new StringBuilder(value.getString()).append("; Domain=").append(domainToSet);
                value.setString(builder.toString());
              } else {
                String newDomain = value.getString().replaceAll("Domain=[A-Za-z0-9.-]*", "Domain=" + domainToSet);
                value.setString(newDomain);
              }
            }
          }
        }
      }
    
      protected String getCookieDomainToSet(String cookieDomain) {
        String[] parts = cookieDomain.split("\\.");
        if (parts.length >= 3) {
          return "." + parts[parts.length - 2] + "." + parts[parts.length - 1];
        }
        return null;
      }
    
      public String toString() {
        return ("CrossSubdomainSessionValve[container=" + container.getName() + ']');
      }
    }
    

    The algorithm works like this:
    – Only if the session is new – find the session cookie
    – Get the requested host name
    – Split the host name with ‘.’
    – If it has at least 3 parts (like http://www.google.de), remove first part (to .google.de)
    – Reset the cookie

    In your Context configuration you can apply the valve like this

    <Valve className="my.package.CrossSubdomainSessionValve" httpOnlyEnabled="true" />
    

    Caveat: In the code the Valve creates a session if no session was created before and does not care if you need a session at all…

    Hope that helps… Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.