我无法从 Grails 3.2.8 中的控制器获取 springSecurityService.currentUser 。
请注意,Grails 3.2.8 进行了一些性能改进,而之前的版本似乎破坏了 Spring Security Core。根据域类中的 Grails 3.2.8 依赖注入,我设置了 grails.gorm.autowire = true,从而解决了该不兼容问题。
重现步骤:
使用角色和组使用 Spring Security 3.1.1 创建一个新的 Grails 3.2.8 Web 应用程序。
在 application.yml 中,设置 grails.gorm.autowire = true
在引导程序中:
def init = { servletContext -> User user = new User(username: 'foo', password:'foo').save(flush: true) Role role = new Role(authority: 'SomeRole').save(flush: true) RoleGroup roleGroup = new RoleGroup(name: 'SomeRole').save(flush: true) RoleGroupRole.create(roleGroup, role) UserRoleGroup.create(user, roleGroup) }
创建一个控制器TestController:
@Secured(['SomeRole']) class TestController { def springSecurityService def index() { println "springSecurityService==null? ${(springSecurityService==null).toString()}" println "SecurityContextHolder.context==null? ${(SecurityContextHolder.context==null).toString()}" println "SecurityContextHolder.context?.authentication==null? ${(SecurityContextHolder.context?.authentication==null).toString()}" println "springSecurityService.principal==null? ${(springSecurityService.principal==null).toString()}" println "springSecurityService.currentUser==null? ${(springSecurityService.currentUser==null).toString()}" render "1" //Render something so we don't get an exception. } }
启动服务器并转到 /test。输出是:
springSecurityService==null? false SecurityContextHolder.context==null? false SecurityContextHolder.context?.authentication==null? true springSecurityService.principal==null? true springSecurityService.currentUser==null? true`
有没有好的解决方法?