2013-09-24 100 views
2

安裝Spring Security的核心不記錄作爲插件然後做快速入門春季安全Grails核心在

這裏是我的用戶領域類

package auth 
class User { 
    def springSecurityService 
    String username 
    String password 
    boolean enabled 
    boolean accountExpired 
    boolean accountLocked 
    boolean passwordExpired 
    static mapping = { 
      // password is a keyword in some sql dialects, so quote with backticks 
      // password is stored as 44-char base64 hashed value 
     password column: '`password`', length: 64 
    } 
    static constraints = { 
     username blank: false, size: 1..50, unique: true 
     password blank: false, size: 8..100 
    } 


    Set getAuthorities() { 
     UserRole.findAllByUser(this).collect { it.role } as Set 
    } 

    def beforeInsert() { 
    encodePassword() 
    } 

    def beforeUpdate() { 
     if (isDirty('password')) { 
     encodePassword() 
     } 
    } 

    protected encodePassword() { 
     password = springSecurityService.encodePassword(password, username) 
    } 
} 

而且我boostrap.groovy是

class BootStrap { 

    def init = { servletContext -> 

    auth.User james = new auth.User(username: 'test', enabled: true, password: 'password') 
    james.save() 
    if (james.hasErrors()) 
    { 
     println("he has errors") 
    } 


    println("we made it! ") 
} 

    def destroy = { 
    } 
} 

但當我登錄時,它會一直說「對不起,我們無法找到具有該用戶名和密碼的用戶。」有什麼想法嗎?

回答

2

這是因爲您在編碼密碼時使用salt

password = springSecurityService.encodePassword(password, username) 

我不知道醃製,因此不能引導你很多。

但是如果你不加入鹽,然後你的代碼作品編碼密碼,只是編碼密碼時刪除用戶名,試試這個

password = springSecurityService.encodePassword(password) 

希望這有助於。

0

您可以驗證用戶是否實際引導到數據庫中?

如果是這樣,我遇到了類似的問題,與Tomcat緩存一些數據不正確。

這裏是我做過什麼:

  1. 停止Tomcat的
  2. 刪除Tomcat的Temp目錄
  3. 重啓動Tomcat的

之後,所有的文件,它工作得很好。

讓我知道這是否有幫助。

+0

所以我使用intellij和使用tomcat插件。還使用內存數據庫。這只是涉及到刪除和添加tomcat插件? – Badmiral

+0

不,我不這麼認爲。但是,另一個人問了一個類似的問題,並說清理該項目幫助:http://stackoverflow.com/questions/18985126/grails-2-3-0-view-rendering-issue/18985225#18985225 –

0

此外,自從我從頭開始構建Grails網站之後,它已經有一段時間了,但我想我記得有些在線說明存在問題。 SpringSecurity可能會爲你編碼密碼,所以當你這樣做時,它會變得雙編碼

嘗試刪除編碼密碼的行。

+0

試過這個,得到了相同的結果:( – Badmiral

1

如果創建BootStrap.groovy用戶,嘗試改變這一點:

def adminUser = User.findByUsername('admin') ?: new User(
    username: 'admin', 
    password: springSecurityService.encodePassword('admin'), 
    enabled: true).save(failOnError: true) 

這樣:

def adminUser = User.findByUsername('admin') ?: new User(
    username: 'admin', 
    password: 'admin', 
    enabled: true).save(failOnError: true) 

的問題是,您所使用的編碼密碼兩次,一次是在域和一次在構造函數的參數中。