2014-01-21 16 views
1

這是有春季安全上顯示一個Spring Security類的屬性中的Grails

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"/>。它給出了一個

沒有這樣的屬性:名字類:org.codehaus.groovy.grails.plugins.springsecurity.GrailsUser

是否有任何其他的方式來顯示當前登錄的其他屬性在用戶?

+0

''用於由spring安全性定義的字段,如用戶名,啓用等,不用於開發人員定義的其他字段。 – user1791574

回答

2

loggedInUserInfo只能訪問來自「主體」的數據,該主體通常是GrailsUser的實例。這些數據包括用戶名,ID,分配的角色名稱以及關於用戶是否已啓用,帳戶是否被鎖定等的一些布爾值。很容易將GrailsUser分類並創建您自己的UserDetailsService,並在用戶域類中捕獲其他數據驗證,並將其存儲在GrailsUser子類中以使其可用於此標記;有關更多信息,請參見http://grails-plugins.github.io/grails-spring-security-core/docs/manual.1273/guide/11%20Custom%20UserDetailsService.html

如果數據是隻讀的,因爲它將被緩存直到用戶註銷或會話過期,所以這很有效。如果你要顯示的數據可以改變,檢索用戶實例,並把它添加到您從控制器動作返回模型地圖:

class MyController { 

    def springSecurityService 

    def theAction() { 
     ... 
     def user = springSecurityService.currentUser 
     [user: user, foo: 5, bar: "whatever", ...] 
    } 
} 

,然後您可以顯示任何你想要的GSP,例如

相關問題