2015-01-21 1 views
1

Я использую Grails 2.4.4 и плагин Spring Security RC4 для создания аутентификации и авторизации моего веб-сайта. Ниже мой класс User:Grails Spring Security, как настроить (или добавить) другие правила аутентификации

class Member { 
    transient springSecurityService 
    long id 
    String loginEmail 
    String username 
    String password 
    String mobilePhone 
    String nickName 
    String lastName 
    String firstName 
    String validateCode 
    String mobilePhoneNo 
    boolean mobilePhoneIsValidated 
    String mobilePhoneValidateCode 
    Picture avatar 
    Date lastSuccessfullyLoginTime 
    Date lastTryingLoginTime 
    int loginFailedCount 
    Date dateCreated 
    Date lastUpdated 
    String createdBy 
    String updatedBy 
    String validateStatus 
    boolean enabled = false 
    boolean accountExpired = false 
    boolean accountLocked = true 
    boolean passwordExpired = true 

    static transients = ['springSecurityService'] 

    static hasMany = [insiderInfos:InsiderInfo, memberEvaluateNews:MemberEvaluateNews, loginHistories:LoginHistory] 

    static constraints = { 
     username     blank: false, unique: true 
     password     blank: false 
     loginEmail     email: true, blank: false, unique: true 
     mobilePhone     blank: false 
     lastName     blank: false 
     firstName     blank: false 
     mobilePhoneNo    nullable: true 
     nickName     nullable:true 
     avatar      nullable: true 
     mobilePhoneIsValidated  nullable:false 
     mobilePhoneValidateCode  nullable: true; 
     lastSuccessfullyLoginTime nullable:true 
     lastTryingLoginTime   nullable: true 
     validateStatus    blank: false //, inList: ValidateStatusEnum.values()*.id 
     createdBy     shared:'varchar20' 
     updatedBy     shared:'varchar20' 
    } 

    static mapping = { 
     comment('會員主檔') 
     id       generator: 'identity' 
     loginFailedCount   comment:'登入錯誤的次數', defaultValue:0 
     password     comment:'密碼',length: 64 
     mobilePhone     comment:'行動電話號碼', length: 20 
     accountLocked    comment:'帳號是否被鎖住' 
     passwordExpired    comment:'帳號是否過期/停用' 
     validateStatus    comment:"驗證狀態,WAIT_VALIDATE,VALIDATE_FAILED,SUCCESS",length: 20 
     validateCode    comment:'驗證碼', length: 64 
     lastTryingLoginTime   comment:'上次嘗試登入的時間' 
     lastSuccessfullyLoginTime comment:'上次成功登入的時間' 
     insiderInfos    comment:'會員提供的內線消息' 
     mobilePhoneIsValidated  comment:'行動電話號碼是否通過驗證', defaultValue:false 
     mobilePhoneValidateCode  comment:'行動電話驗證碼',length:10 
     mobilePhoneNo    comment:'行動電話號碼', length: 20 
     username     comment:'user name', length: 20 
     nickName     comment:'假名', length:20 
     lastName     comment:'姓氏', length: 10 
     firstName     comment:'名字', length: 20 
     avatar      comment:'大頭照編號(在 Picture 裡的號碼)' 
     createdBy     length: 20 
     updatedBy     length: 20 
     //password column: '`password`',comment:'密碼',length: 64 
    } 


    Set<Role> getAuthorities() { 
     MemberRole.findAllByMember(this).collect { it.role } 
    } 

    def beforeInsert() { 
     encodePassword() 
    } 

    def beforeUpdate() { 
     if (isDirty('password')) { 
      encodePassword() 
     } 
    } 
    String fullName() { 
     return "${lastName + firstName}" 
    } 

    protected void encodePassword() { 
     password = springSecurityService?.passwordEncoder ? springSecurityService.encodePassword(password) : password 
    } 

    ValidateStatusEnum getValidateStatus() { 
     return validateStatus? ValidateStatusEnum.byId(validateStatus): null 
    } 

    void setValidateStatus(ValidateStatusEnum localValidateStatusEnum) { 
     validateStatus = localValidateStatusEnum.id 
    } 
} 

Помимо встроенного правила аутентификации весной Security, мне нужно еще одно правило проверки подлинности, который:

  1. член может войти в систему с любого пользователя или loginEmail

Как добавить правило выше?

+0

Try [это] (http://www.jellyfishtechnologies.com/grails-spring-security-login-with- Имя пользователя или адрес электронной почты, не обращая внимания-регистр) – user1690588

ответ

0

вы должны перейти к выборочному UserDetailService, как описано в spring-core docs:

class UsernameOrEmailUserDetailsService implements GrailsUserDetailsService { 

    UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { 
    User.withTransaction { status -> 
     User user = User.findByUsernameOrLoginEmail(username, username) 
     return new GrailsUser(user.username, user.password, ..., user.id) 
     } 
    } 
    } 
} 
Смежные вопросы