这是我的具有Spring Security的用户类
package rms
import java.util.Date;
import java.util.Set;
import enums.EmployeeStatus;
class User {
transient springSecurityService
String username
String password
boolean enabled
boolean accountExpired
boolean accountLocked
boolean passwordExpired
String firstName
String lastName
String middleName
String contactNumber
String position
String emailAddress
String employeeID
Date dateOfBirth
EmployeeStatus employeeStatus
int age
byte [] picture
static hasMany = [employeeReport: EmployeeReport]
static constraints = {
picture maxSize:20* 1024 * 1024
dateOfBirth nullable: true
employeeStatus blank: false
position blank: false
contactNumber blank: false
emailAddress blank: false, matches: "([a-z0-9_.-]+)@([da-z.-]+).([a-z.]{2,6})", email: true
age min: 18
username blank: false, unique: true
password blank: false, password: true
}
static mapping = { password column: '`password`' }
Set<SecRole> getAuthorities() {
SecUserSecRole.findAllBySecUser(this).collect { it.secRole } as Set
}
def beforeInsert() {
encodePassword()
}
def beforeUpdate() {
if (isDirty('password')) {
encodePassword()
}
}
protected void encodePassword() {
password = springSecurityService.encodePassword(password)
}
String toString(){
return "SecUser $username"
}
}
我尝试了这个标签
<sec:loggedInUserInfo field="username"/>,它可以正常工作,但是这不起作用
<sec:loggedInUserInfo field="firstName"/>。它给出了
No such property: firstName for class: org.codehaus.groovy.grails.plugins.springsecurity.GrailsUser
还有其他方法可以显示当前登录用户的其他属性吗?
请您参考如下方法:
loggedInUserInfo仅可以访问“principal”中的数据,“principal”通常是GrailsUser的实例。数据包括用户名,ID,分配的角色名称,以及有关是否启用用户,锁定帐户等的一些 bool(boolean) 值。很容易将GrailsUser子类化并创建自己的UserDetailsService并在此期间从用户域类捕获其他数据身份验证,并将其存储在GrailsUser子类中以使其可用于此标签;有关更多信息,请参见http://grails-plugins.github.io/grails-spring-security-core/docs/manual.1273/guide/11%20Custom%20UserDetailsService.html。
如果数据是只读的,这将很好地工作,因为它将被缓存直到用户注销或 session 到期为止。如果要显示的数据可以更改,请检索用户实例并将其添加到从 Controller 操作返回的模型图中:
class MyController {
def springSecurityService
def theAction() {
...
def user = springSecurityService.currentUser
[user: user, foo: 5, bar: "whatever", ...]
}
}
然后您可以在GSP中显示所需内容,例如
${user.firstName}




