2009-10-31 48 views

回答

3

恕我直言,這是目前不是Acegi的插件的一部分。我向LoginController添加了一個忘記的密碼操作:

def forgotPassword = { 
    if (params.username) { 
     User user = User.findByUsername(params.username) 
     if (user) { 
      def password = randomService.generateRandomString(8) 
      user.passwd = authenticateService.encodePassword(password) 
      if (!user.save(flush:true)) { 
       user.errors.each { 
        log.error "err $it" 
       } 
       flash.message = message(code: "LoginController.msg.forgot.error") 
      } else { 
       sendMail { 
        to user.username 
        subject message(code:"LoginController.mail.forgot.subject") 
        body(view:"forgotPasswordEmail", model: [person:user, password:password]) 
       } 
       flash.message = message(code:"LoginController.msg.forgot", args:[user.username]) 
      } 
     } else { 
      flash.message = message(code:"LoginController.msg.forgot.unknown", args:[params.username]) 
     } 
    } 
} 

上面的代碼使用Grails郵件插件。

+2

這是一個很好的開始實施。一個潛在的問題是它會自動重置密碼,即使其他人請求密碼也是如此,所以惡意用戶有可能不斷地點擊「重置密碼」並更改用戶的密碼,但實際上並不希望密碼被更改(儘管它們「 d仍然收到電子郵件)。 我們做了這樣的事情,但是有一張桌子只有一段時間使用令牌,短時間通過電子郵件發送來重置密碼。如果令牌未使用,則密碼不變。表中每個用戶最多隻能有一個令牌。 – 2009-10-31 17:40:59

+0

完全同意。另一種選擇是人類網站的一種安全問題,例如「你母親的孃家姓什麼?」。只有正確回答問題,密碼纔會重置。也許這是有道理的,以取決於acegi插件的獨立插件中的賬戶處理函數「恢復密碼」,「恢復登錄名」,「刪除賬戶」。 – 2009-11-01 17:08:20

3

谷歌正在失敗,因爲沒有一個。真正無法扭轉哈希密碼(沒有暴力破解和彩虹表),如果是,那就意味着你的系統不安全。

常見模式是用一次性使用令牌通過電子郵件發送忘記密碼的用戶,然後用它們將密碼重置爲任何他們想要的。這不是內置到框架中的,但手動操作並不難(我建議使用grails郵件插件)。

3

Acegi插件不支持開箱即用,但如果添加email-confirmation插件,則很容易推出自己的插件。

步驟如下:

創建要求用戶輸入他們的電子郵件地址和新密碼密碼重置形式。

處理密碼重置表單的控制器操作應該驗證數據,並且 使用電子郵件確認插件向用戶發送電子郵件並提供鏈接供他們單擊以確認其密碼更改。您可以通過在插件添加的EmailConfirmationService服務上調用以下方法來完成此操作。

def sendConfirmation(String emailAddress, String theSubject, Map model = null, 
String userToken = null) 

其中:

emailAddress = address of user changing password 
theSubject = subject of e-mail sent 
model = any data passed to GSP that creates e-mail body 
userToken = hashed user's password 

當用戶點擊電子郵件中的鏈接(請參閱有關如何自定義此電子郵件信息的插件文檔),該onConfirmation關閉該服務將被調用。

此截止時間應在Bootstrap.groovy分配是這樣的:

def emailConfirmationService 

def init = { servletContext -> 

    emailConfirmationService.onConfirmation = { email, hashedPassword -> 

    User user = User.findByEmail(email) 
    user.passwd = hashedPassword 
    if (!user.save()) { 
     // Handle this error, somehow.... 
    } 

    // Then return a map which will redirect the user to the login screen (for example) 
    [controller:'userProfile', action:'login'] 
    } 
} 

注意用戶的電子郵件和哈希密碼被傳遞到這個閉合,使您能夠重置並保存用戶的密碼。