我在我的项目中使用 Grails 2.1.1,现在我正在使用 springSecurityService.currentUser 来获取用户凭据等。
在过去的两天里,我的项目需要一些 CMS 扩展,我偶然发现了 Weceem 插件。在这里和那里设置东西,最后我的带有 Weceem 插件的项目现在正在运行,但是每次调用 springSecurityService.currentUser 方法时都会出现空指针异常。
没有 weceem grails-plugin 一切都运行良好,所以我假设我需要进行一些设置。问题是在哪里和什么?
这是我的用户类
class User {
transient springSecurityService
String username
String password
boolean enabled = true
boolean accountExpired = false
boolean accountLocked = false
boolean passwordExpired = false
Person person
static hasOne = [Person]
static hasMany = [roles: Role]
static constraints = {
username blank: false, unique: true
password blank: false
}
static mapping = {
password column: '`password`'
}
Set<Role> getAuthorities() {
roles as Set
}
def beforeInsert() {
encodePassword()
}
def beforeUpdate() {
if (isDirty('password')) {
encodePassword()
}
}
protected void encodePassword() {
password = springSecurityService.encodePassword(password)
}}
这是我调用 springSecurityService 的控制器
//show the list of all person
def list = {
//get all the sorting params
params.sort = params.sort ?: 'firstName'
params.order = params.order ?: 'asc'
params.max = params.max ?: 10
params.offset = params.offset ?: 0
def test = springSecurityService.getCurrentUser()
def personList = Person.createCriteria().list (max: params.max, offset: params.offset) {
if (springSecurityService.currentUser.person.affiliate.value != 'Admin'){
eq("affiliate", springSecurityService.currentUser.person.affiliate)
eq("deleted", false)
}
order(params.sort, params.order)
}
render view:'list', model:[persons: personList, personTotal: personList.totalCount]
}