Grails 3.3.8-如何从服务获取会话

时间:2019-06-10 13:09:47

标签: grails grails3

好吧,我有一个多模块项目,其中的服务包含在插件中,还有多个使用它的Web“应用程序”。

用户和播放器服务具有登录和注册方法,该方法需要通过更新会话来“登录”播放器。

此帖子:Grails get Session and Management in Service class

有一些解决方案,但似乎没有一个有效。这些位置均无法使用Webutils

import org.codehaus.groovy.grails.web.util.WebUtils
import org.grails.web.util.WebUtil
import org.grails.web.util.WebUtils

这项工作也没有:

def session = getSession(true)

有什么建议吗?

这来自插件的build.gradle:

dependencies {
    compile "org.springframework.boot:spring-boot-starter-logging"
    compile "org.springframework.boot:spring-boot-autoconfigure"
    compile "org.grails:grails-core"
    compile "org.grails.plugins:events"
    compile "org.grails.plugins:hibernate5"
    compile "org.hibernate:hibernate-core:5.1.5.Final"
    compile "org.grails.plugins:scaffolding"
    console "org.grails:grails-console"
    profile "org.grails.profiles:plugin"
    provided "org.grails:grails-plugin-services"
    provided "org.grails:grails-plugin-domain-class"
    runtime "org.glassfish.web:el-impl:2.1.2-b03"
    runtime "com.h2database:h2"
    runtime "org.apache.tomcat:tomcat-jdbc"
    testCompile "org.grails:grails-gorm-testing-support"
    testCompile "org.grails:grails-plugin-testing"
}

1 个答案:

答案 0 :(得分:1)

  

有什么建议吗?

是的

您可以这样做:

// grails-app/services/somepackage/SomeService.groovy
package somepackage

import grails.web.api.ServletAttributes

class SomeService implements ServletAttributes {

    void someMethod() {
        // you can access the session property directly from here
        println session

        // ...
    }
}
相关问题