如何在Grails Bootsrap中添加启用了审核跟踪的用户?

如何在Grails Bootsrap中添加启用了审核跟踪的用户?,grails,spring-security,bootstrapping,audit-trail,Grails,Spring Security,Bootstrapping,Audit Trail,嗨,我很难在grails引导中添加第一个用户。我尝试了许多不同的方法,但都失败了,因为Audit Trail插件希望用户标记createdBy和EditedBy字段,但第一个还没有退出 这是我的审核跟踪配置,基本上与默认配置相同,只是我在User.id上使用User.username: grails { plugin { audittrail { createdBy.field = "createdBy" createdB

嗨,我很难在grails引导中添加第一个用户。我尝试了许多不同的方法,但都失败了,因为Audit Trail插件希望用户标记createdBy和EditedBy字段,但第一个还没有退出

这是我的审核跟踪配置,基本上与默认配置相同,只是我在User.id上使用User.username:

grails {
    plugin {
        audittrail { 
            createdBy.field = "createdBy"
            createdBy.type   = "java.lang.String" //Long is the default
            editedBy.field = "editedBy"
            editedBy.type   = "java.lang.String" //Long is the default
            createdDate.field = "createdDate"
            editedDate.field = "editedDate"

            currentUserClosure = {ctx->
                def authPrincipal = ctx.springSecurityService.principal
                if(authPrincipal && authPrincipal != "anonymousUser"){
                    return authPrincipal.username
                } else {
                    return null //fall back
                }
            }
        }
    }
}
下面是我的用户类的外观,即,我只是添加了注释以使插件工作:

@gorm.AuditStamp
class User {
  //... basic Spring Security Generated User File with minor changes
}
以下是我的引导文件的内容:

def adminUser = new User(
    username: 'admin',
    enabled: true,
    password: 'password',
    firstName: 'ADMIN',
    lastName: 'ADMIN'
).save(flush: true)

// The below will work if I take off the annotation on User class.
SpringSecurityUtils.doWithAuth('admin') {
    def adminRole = new Role(authority: 'ROLE_ADMIN').save(flush: true)
    UserRole.create adminUser, adminRole, true
}
那么现在我如何添加第一个用户呢?我试过:

- Using annonymousUser
    - Failed, plugin prevents it
- Hardcoding the createdBy and editedby to "admin"
    - Failed get's overridden 
- Use executeUpdate to insert the user directly in the DB
    - Failed: in bootstrap
- Delay the save of the first user until the doWithAuth
    - Failed: couldn't find the user

任何帮助都会很好!谢谢

我也有同样的问题,正在寻找解决办法。我通过执行以下操作使其正常工作:

grails {
plugin {
audittrail {
createdBy.field = "createdBy"
createdBy.type   = "java.lang.String" //Long is the default
editedBy.field = "modifiedBy"
editedBy.type   = "java.lang.String" //Long is the default
createdDate.field = "createdDate"
editedDate.field = "modifiedDate"

//custom closure to return the current user who is logged in
currentUserClosure = {ctx->
//ctx is the applicationContext
def userName = ctx.springSecurityService.principal?.username
return userName != null ? userName : "System"
}
}
}
}
在Bootstrap.groovy文件中,只需执行如下保存操作:

def adminUser = new User(username: 'admin', enabled: true, password: 'password',
firstName: 'ADMIN', lastName: 'ADMIN').save(flush: true)
def adminRole = new Role(authority: 'ROLE_ADMIN').save(flush: true)
UserRole.create adminUser, adminRole, true

新创建的第一个用户将加盖用户“系统”的印章(createdBy和modifiedBy列)。

谢谢!我试着用一个非用户来做,但失败了,我从来没有想过要把它改成另一个字符串!