【问题标题】:Grails and Spring Security: How catch user into controller?Grails 和 Spring Security:如何将用户捕获到控制器中?
【发布时间】:2014-02-19 12:26:48
【问题描述】:

我阅读了thisthisthis,但没有解决我的问题。

这是我的错误:
无法在空对象上调用 getCurrentUser() 方法。 Stacktrace 如下:
消息:无法在 null 对象上调用方法 getCurrentUser()

我的代码:

import org.springframework.dao.DataIntegrityViolationException
import grails.plugin.springsecurity.annotation.Secured;

@Secured('ROLE_SYSTEMADMIN')
class InstanceController {

...

def save() {
    def instanceInstance = new Instance(params)
def springSecurityService

println("user is"+springSecurityService.getCurrentUser());
...

我用:

  • GGTS 3.4.0。
  • 编译':spring-security-core:2.0-RC2'

我已登录。我确定。 谢谢回复。

拉齐尔

【问题讨论】:

    标签: spring grails spring-security


    【解决方案1】:

    服务没有被注入,因为您没有声明为控制器的全局属性。从你的保存方法中声明它,然后调用 currentuser 属性,这应该可以工作。

    【讨论】:

      【解决方案2】:

      您将springSecurityService 定义为方法变量。您应该将其定义为实例变量:

      class InstanceController {
        def springSecurityService
        def save() {
          def instanceInstance = new Instance(params)
          println("user is"+springSecurityService.getCurrentUser());
          ..
        }
      }
      

      现在 spring 可以在创建控制器后注入springSecurityService

      【讨论】:

      • 值得注意的是,每个控制器中都注入了一个方法:getAuthenticatedUser(),其工作方式相同。它也可以像控制器属性一样访问:authenticatedUser。提到了here
      猜你喜欢
      • 2014-02-13
      • 2021-06-09
      • 2010-10-20
      • 2011-12-12
      • 2014-04-22
      • 2019-06-01
      • 2014-06-10
      • 2017-03-20
      • 1970-01-01
      相关资源
      最近更新 更多