【问题标题】:How to get the same session across subdomains in grails如何在grails中跨子域获得相同的会话
【发布时间】:2012-06-02 05:06:38
【问题描述】:

我有一个应用程序处理多个子域,例如

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

用户在使用应用程序时可以在这些子域之间切换。发生这种情况时,会话不会在这些子域之间共享。我使用 tomcat 作为开发和生产服务器。

我正在尝试使共享会话首先在开发中工作。在阅读时,发现在tomcat中实现这一点的方法是:

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

有没有办法可以在开发环境的tomcat中设置这个?

我在 _Events.groovy 中尝试了以下代码,但没有成功:

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

我收到错误 java.lang.IllegalArgumentException: addChild: Child name '' is not unique

我假设我需要的是以下代码的等效项(由于没有 getContext 方法,因此无法工作):

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

关于如何在开发和生产中实现此功能的任何建议? 提前感谢您的帮助。

【问题讨论】:

    标签: tomcat grails subdomain session-cookies


    【解决方案1】:

    要访问默认的 Tomcat 上下文,您可能需要在插件的 TomcatServer.groovy 文件中修补 TomcatServer 的 create 方法。

    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()
    }
    

    【讨论】:

    • Valbuena, TomcatServer.groovy 文件不在插件中。插件升级为使用 Tomcat7 (github.com/grails-plugins/grails-tomcat-plugin) 时已将其删除。
    • 我首先尝试修改tomcat插件版本1.3.9。但这与一个 tomcat 版本捆绑在一起,其中 setSessionCookieDomain 未在 StandardContext 类中实现。因此我尝试了 2.0.4 版本,TomcatServer 类似乎已被删除。
    猜你喜欢
    • 1970-01-01
    • 2012-08-04
    • 2014-09-25
    • 2014-05-10
    • 2023-03-15
    • 2012-01-20
    • 2012-10-05
    • 1970-01-01
    • 2017-11-22
    相关资源
    最近更新 更多