我试图在 Grails 3.0.3 应用程序中的 Controller 中获取当前用户。
我已经使用 this repo 作为我的安全设置的基础 - 安全是基于 GORM 的。我在 build.gradle 中使用以下行以包含 Spring Security Framework:

compile "org.springframework.boot:spring-boot-starter-security"

但是当我尝试像在 Controller 中的其他 SO 线程(参见例如: this one )中推荐的那样注入(inject) springSecurityService 时,我只得到一个空对象。它没有像它应该的那样启动。
class RestapiController {
    def springSecurityService

    def currentUser(){
        def user = springSecurityService.currentUser
        render user
    }
}

如何将 springSecurityService 注入(inject) Grails 3.0.3 中的 Controller ?

更新:
最后,我使用以下行来获取当前用户:
SecurityContextHolder.context.authentication.name

最佳答案

springSecurityService 不是 Spring Security 的一部分,它在 Grails spring-security-core 插件中。 Spring Security 没有“当前用户”的概念。您可以访问当前的 Authentication 并获取用户名、密码、启用等,但框架中没有任何内容可以让您返回用于填充身份验证的源对象(在 Grails + spring-security-core 中,这是通常是 User 域类实例) - 这必须在您的应用程序代码中完成。

本周末,我发布了该插件的初始版本,可与 Grails 3 版本 3.0.0.M1 配合使用。文档是 here 。文档中有一个简短的教程可以帮助您入门,您也可以查看 this sample app using the plugin in Grails 3

关于spring - 使用 Grails 3.0.3 将 springSecurityService 注入(inject) Controller ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32053366/

10-14 11:25
查看更多