2013-08-20 15 views
1

我正在尋找使用用戶名/密碼組合進行身份驗證的Grails應用程序以使用電子郵件/密碼。我試圖對AuthControler.groovy和ShiroDbReal.groovy進行更改以允許此操作,但它不允許我登錄。使用電子郵件地址作爲唯一標識符而不是Shiro for Grails的用戶名

這裏是我的模型類

class User { 
String email 
String passwordHash 
byte[] passwordSalt 


static belongsTo = Account 
static hasMany = [ roles: Role, permissions: String ] 

static constraints = { 
    email (nullable: false, blank: false, unique: true) 
} 
} 

這裏是我的AuthController簽到功能

def signIn = { 
    def authToken = new UsernamePasswordToken(params.email, params.password as String) 

    if (params.rememberMe) { 
     authToken.rememberMe = true 
    } 

    def targetUri = params.targetUri ?: "/" 

    def savedRequest = WebUtils.getSavedRequest(request) 
    if (savedRequest) { 
     targetUri = savedRequest.requestURI - request.contextPath 
     if (savedRequest.queryString) targetUri = targetUri + '?' + savedRequest.queryString 
    } 

    try{ 
     SecurityUtils.subject.login(authToken) 
     redirect(uri: targetUri) 
    } 
    catch (AuthenticationException ex){ 
     log.info "Authentication failure for user '${params.username}'." 
     flash.message = message(code: "login.failed") 
     redirect(action: "login", params: m) 
    } 
} 

最後我ShiroDbReal.groovy

def authenticate(authToken) { 
    log.info "Attempting to authenticate ${authToken.username} in DB realm..." 
    def email = authToken.username 

    if (email == null) { 
     throw new AccountException("Null usernames are not allowed by this realm.") 
    } 

    def user = User.findByEmail(email) 
    if (!user) { 
     throw new UnknownAccountException("No account found for user [${username}]") 
    } 

    log.info "Found user '${user.username}' in DB" 
    def account = new SimpleAuthenticationInfo(email, user.passwordHash, new SimpleByteSource(user.passwordSalt), "ShiroDbRealm") 

    if (!credentialMatcher.doCredentialsMatch(authToken, account)) { 
     log.info "Invalid password (DB realm)" 
     throw new IncorrectCredentialsException("Invalid password for user '${username}'") 
    } 

    return account 
} 

我recieving一個「groovy.lang .MissingPropertyException:在調試器中沒有這樣的屬性:類的用戶名:icango.User'異常e'SecurityUtils.subject.login(authToken)'方法調用。

我真的不知道還有什麼地方可以找,因爲我找不到任何有關更改唯一標識符的文檔。任何幫助真的會被讚賞。

回答

2

你忘了更改以下行:

log.info "Found user '${user.username}' in DB" 

刪除,或將其更改爲:

log.info "Found user '${user.email}' in DB" 

應該修復它。

+0

由於日誌記錄已關閉,我不認爲這些語句會得到評估,我錯了。感謝您的高舉。 –

0

對於所有例外情況也是如此......您應該將所有用戶名出現更改爲電子郵件。

相關問題