How to get the same session on subdomains in grails

I have one application that processes multiple subdomains, such as

  • sub1.domain.com
  • sub2.domain.com
  • www.domain.com
  • domain.com

The user can switch between these subdomains when using the application. When this happens, the session is not split between these subdomains. I use tomcat as a server for development and production.

I try to create shared sessions in development first. Upon reading, it turned out that the way to achieve this in tomcat is:

<Context sessionCookiePath="/" sessionCookieDomain=".domain.com">

Is there a way to install this on tomcat in a development environment?

I tried the code below in _Events.groovy, without success:

eventConfigureTomcat = {tomcat ->
    def context = tomcat.addContext("","/")
    context.setSessionCookieDomain(".domain.com")
    context.setSessionCookiePath("/")
}

I get java.lang.IllegalArgumentException: addChild: child name 'error is not unique

, ( , getContext):

eventConfigureTomcat = {tomcat ->
    def context = tomcat.getContext("") //This function does not exist
    context.setSessionCookieDomain(".domain.com")
    context.setSessionCookiePath("/")
}

, , ? .

+5
1

Tomcat , , , TomcatServer TomcatServer.groovy .

TomcatServer(String basedir, String webXml, String contextPath, ClassLoader classLoader) {
    tomcat = new Tomcat()
this.buildSettings = BuildSettingsHolder.getSettings()  

    if(contextPath=='/') contextPath = ''

    def tomcatDir = new File("${buildSettings.projectWorkDir}/tomcat").absolutePath
    def ant = new AntBuilder()      
    ant.delete(dir:tomcatDir, failonerror:false)        

    tomcat.basedir = tomcatDir

    context = tomcat.addWebapp(contextPath, basedir) 
    // ** do additional context stuff here ** 
    tomcat.enableNaming()       

    // we handle reloading manually
    context.reloadable = false
    context.setAltDDName("${buildSettings.projectWorkDir}/resources/web.xml")

    def aliases = []
    def pluginManager = PluginManagerHolder.getPluginManager()
    def pluginSettings = GPU.getPluginBuildSettings()
    if(pluginManager!=null) {
        for(plugin in pluginManager.userPlugins) {
              def dir = pluginSettings.getPluginDirForName(GrailsNameUtils.getScriptName(plugin.name))
              def webappDir = dir ? new File("${dir.file.absolutePath}/web-app") : null
              if (webappDir?.exists())
                    aliases << "/plugins/${plugin.fileSystemName}=${webappDir.absolutePath}"
    }
}

    if(aliases) {
        context.setAliases(aliases.join(','))
    }
    TomcatLoader loader = new TomcatLoader(classLoader)

    loader.container = context
    context.loader = loader

    initialize()
}
0

All Articles