2012-01-24 61 views
4

所以我敲我的頭撞在牆上試圖讓彈簧安全核心1.2.7.1使用Grails 2.0工作...Grails的Spring的安全核心插件 - 無法驗證用戶

我已經看過在教程中運行s2。閱讀新的插件,你對密碼進行加密,所以我舉的樣子:

def userRole = Role.findByAuthority('ROLE_USER') ?: new Role(authority: 'ROLE_USER').save(failOnError: true) 
    def adminRole = Role.findByAuthority('ROLE_ADMIN') ?: new Role(authority: 'ROLE_ADMIN').save(failOnError: true) 

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

     def testUser = User.findByUsername('test') ?: new User(
      username: 'test', 
      password: "test", 
      enabled: true).save(failOnError: true) 

    if (!adminUser.authorities.contains(adminRole)) { 
     UserRole.create adminUser, adminRole 
    } 

    if (!testUser.authorities.contains(userRole)) { 
     UserRole.create testUser, userRole 
    } 

我可以看看H2數據庫,我看到的用戶,其編碼的密碼,查看正在創建的角色,並可以看到用戶角色映射也可以正確創建。

但是,我仍然得到「對不起,我們無法找到具有該用戶名和密碼的用戶。」在兩個用戶的登錄提示符下。

我已經打開了的log4j調試「org.springframework.security」,但我真正走出日誌是:

2012-01-23 23:08:44,875 ["http-bio-8080"-exec-5] DEBUG dao.DaoAuthenticationProvider - Authentication failed: password does not match stored value 
+0

潮紅可能有時幫助,你應該嘗試! –

回答

2

我看不出什麼明顯的錯誤與您的代碼。我使用的Grails的相同版本和彈簧安全插件,並在Bootstrap.groovy作品下面的代碼對我來說:

def init = { servletContext -> 

    // create some roles 
    def userRole = createRole('ROLE_USER') 
    def adminRole = createRole('ROLE_ADMIN') 

    // create some users 
    User admin = createUser('Admin', '[email protected]', adminRole) 
    User user = createUser('User', '[email protected]', userRole) 
} 

private User createUser(name, username, role) { 

    def defaultPassword = 'password' 

    User user = User.findByUsername(username) ?: new User(
      name: name, 
      username: username, 
      password: defaultPassword, 
      passwordConfirm: defaultPassword, 
      enabled: true).save() 

    if (!user.authorities.contains(role)) { 
     UserRole.create user, role 
    } 
    user 
} 

private createRole(String roleName) { 
    Role.findByAuthority(roleName) ?: new Role(authority: roleName).save() 
} 
+0

Ug ...這是殺了我......我甚至通過插件創建GrailsUser ...它正在數據庫中找到用戶並傳遞適當的信息... – user1085751

+0

這似乎是一個問題多個數據源。只要我註釋了其他數據源(它不應該使用它),它就起作用了....不好。 – user1085751

-3

在OP的原代碼,爲什麼是單引號的用戶名和雙密碼引號。這可能是問題所在。

0

我的確有類似的問題。是因爲我忘記添加

grails.plugin.springsecurity.userLookup.userDomainClassName ='yourpackage.User' 
grails.plugin.springsecurity.userLookup.authorityJoinClassName =yourpackage.UserRole' 
grails.plugin.springsecurity.authority.className ='yourpackage.Role' 

之後,驗證工作。

相關問題