2

我正在使用Spring Security插件和Spring Security UI插件。使用用戶名作爲電子郵件,Spring Security UI插件

他們都喜歡魅力,但我的問題是,我不希望我的用戶有一個用戶名,而是他/她應該使用電子郵件作爲用戶名。

所以在寄存器階段我嘗試刪除從註冊頁面的用戶名和設置

newUser.username = params.email 

因此,該插件還可以訪問變量的用戶名,但是這給了我驗證錯誤,我只是能找不到解決問題的辦法。

任何想法?有沒有人做過類似的事情?

+0

你會得到什麼樣的驗證錯誤? – micha

+0

這是我得到的第一個錯誤:「字段'username'的對象'grails.plugins.springsecurity.ui.RegisterCommand'中的字段錯誤:被拒絕的值[null];」 當我嘗試通過執行user.username = params.email(從參數中獲取電子郵件)嘗試設置電子郵件時,我仍然得到相同的結果。 – marko

+0

那麼..錯誤信息說'username'是'null'。所以你應該確保在'params.email'有一個有效的電子郵件。 – micha

回答

4

可以修改Spring Security使用

默認用戶領域在你Config.groovy中添加以下

grails.plugins.springsecurity.userLookup.usernamePropertyName = 'XXX'

其中XXX是要使用的用戶實體的字段

您可以檢查配置屬性here

+0

AAA級鏈接!聽起來很合理,但是當我使用UI插件時,有一些內部類,例如RegistrationCommand,使用「用戶名」。它也用於忘記密碼和帳戶確認。 也許我應該刪除每一個用戶名的痕跡,但我害怕的是某處某處預計該用戶名應該存在。 – marko

+1

最有可能你已經看到[這](http://grails.1312388.n4.nabble.com/Using-spring-security-UI-with-customized-user-class-td2330737.html),有相同的代碼在這個(過時的)線程中關於如何使用具有自定義用戶類的Spring安全UI的示例。 – dimcookies

0

我最近通過將視圖和控制器從spring security plugin複製到我的grails-app並更新視圖來顯示emailAddress反對用戶名。我還用User domain class中的emailAddress替換了用戶名屬性。請確保您更新的約束如下:

static constraints = { 
    emailAddress email: true, blank: false, unique: true 
    password blank: false, password: true 
} 

什麼建議dimcookies是正確的,適用於舊版本的Grails,新的語法結構,至少Grails的2.3.1,是:

grails.plugin.springsecurity.userLookup.usernamePropertyName = 'emailAddress'

相關問題