1

我正在使用 Grails 2.2.2、审计跟踪插件 2.0.3 和 spring-security-core 1.2.7.3

当我将注释放在一个类上并使用浏览器(通过 Controller/gsp)插入记录时,一切正常。

@gorm.AuditStamp
class Note {
   String name
}

但是,当我在 Bootstrap 中插入记录时

new Note(name:'Testing').save()

我在启动时出错

ERROR property.BasicPropertyAccessor  - IllegalArgumentException in class: test.Note, setter method of property: createdBy
ERROR property.BasicPropertyAccessor  - expected type: java.lang.Long, actual value: java.lang.Integer
ERROR context.GrailsContextLoader  - Error initializing the application: IllegalArgumentException occurred while calling setter of test.Note.createdBy; nested exception is org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of test.Note.createdBy

有没有办法修复在 Bootstrap 期间使审计跟踪工作?我只是在从 Bootstrap 插入时放置默认值,并在使用浏览器屏幕插入时放置登录用户。我使用了以下更改,但仍然没有运气:

static mapping = {
    createdBy (defaultValue: Long.valueOf( 1l ))
    editedBy (defaultValue: Long.valueOf( 1l ))
}
static constraints = {
    createdBy nullable:true
    editedBy nullable:true
}

我仍然得到相同的 Long/Integer 错误

编辑:

这是与此 pkugin 相关的我的 Config.groovy 的内容

grails {
    plugin{
        audittrail{
            createdBy.field = "createdBy"
            editedBy.field = "editedBy"
            createdDate.field = "createdDate"
            editedDate.field = "editedDate"
        }
    }
}
4

1 回答 1

4

您可以SpringSecurityUtils.doWithAuth用来伪造安全上下文

import org.codehaus.groovy.grails.plugins.springsecurity.SpringSecurityUtils

// set up a default user, if one doesn't already exist
def defaultUser = User.findByUsername('default') ?: new User(username:'default').save()

// run the following code as if that user were logged in
SpringSecurityUtils.doWithAuth('default') {
  new Note(name:'Testing').save()
}
于 2013-07-10T14:47:04.110 回答