【问题标题】:Injection of springSecurityService does not work注入springSecurityService不起作用
【发布时间】:2016-11-08 11:17:36
【问题描述】:

在以下来自控制器的代码 sn-p 中,我在尝试访问 springSecurityService.currentUser 时收到空指针异常。我希望 def springSecurityService 自动注入服务,我错过了什么?

@Transactional(readOnly = true)
@Secured('ROLE_USER')
class TaskController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
def springSecurityService
def user = springSecurityService.currentUser

【问题讨论】:

    标签: grails grails-spring-security


    【解决方案1】:

    将其他 Spring bean 注入到您的控制器或服务中是在类级别而不是在方法中完成的。

    例如,您的代码应如下所示:

    @Transactional(readOnly = true)
    @Secured('ROLE_USER')
    class TaskController {
    
        static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
        def springSecurityService // inject the bean here, at the class level
    
        def index(Integer max) {
            params.max = Math.min(max ?: 10, 100)
            def user = springSecurityService.currentUser
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-08
      • 2015-11-10
      • 2014-11-18
      • 2013-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-11
      相关资源
      最近更新 更多