我正在使用带有Spring Security 1.2.7.1和Spring Security UI 0.2的Grails 2.1.1。
当我使用内存数据库在BootStrap.groovy中创建一些测试用户时,我能够登录并管理用户 - 一切正常。但是,当我将数据源切换到MySQL数据库时,该服务指出在我尝试登录时禁用了用户帐户。我已经检查并且密码似乎正确哈希(即,它与数据库中的内容匹配) 。我对用户进行的唯一修改是包含一个研究ID。在尝试调试代码时,我发现org.codehaus.groovy.grails.plugins.springsecurity.GrailsUser表示enabled = false。
非常感谢任何帮助!
我的数据源:
dataSource {
driverClassName = "com.mysql.jdbc.Driver"
dialect = org.hibernate.dialect.MySQL5InnoDBDialect
url = "jdbc:mysql://localhost/users"
username = "root"
password = ""
dbCreate = "create-drop"
}
我的BootStrap.groovy代码:
def adminRole = new Role(authority: 'ROLE_ADMIN').save(flush: true)
def userRole = new Role(authority: 'ROLE_USER').save(flush: true)
def testAdmin = new User(username: 'me', enabled: true, password: 'password', studyID: '0')
testAdmin.save(flush: true)
UserRole.create testAdmin, adminRole, true
def testUser = new User(username: '100', enabled: true, password: 'test', studyID: '101')
testUser.save(flush: true)
UserRole.create testUser, userRole, true
assert User.count() == 2
assert Role.count() == 2
assert UserRole.count() == 2
启动应用程序后,我的User.user表中的一行。无论我如何更改这些值,登录尝试都会声明用户已被禁用:
id version account_expired account_locked enabled password password_expired studyid username
1 0 1 1 1 5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a... 1 0 me
答案 0 :(得分:2)
如果直接对DB进行更改也会出错并且它与'内存数据库'一起使用,我认为MySQL可能就是问题。
可能是MySQL使用Hibernate无法正确使用的类型存储值boolean
您可以通过从数据库中检索用户并手动检查来测试:
if( ! user.enabled ) println "not enabled" // or something like that
this mail list上的最后3篇帖子提供了在MySQL中正确映射boolean
的解决方案:
您可以使用自定义方言将BIT(1)更改为布尔值:
import org.hibernate.dialect.MySQL5InnoDBDialect
import java.sql.Types
class MyCustomMySQL5InnoDBDialect extends MySQL5InnoDBDialect {
MyCustomMySQL5InnoDBDialect() {
registerColumnType(Types.BIT, 'boolean')
}
}
并使用它在DataSource.groovy中指定类:
dataSource {
pooled = true
driverClassName = 'com.mysql.jdbc.Driver'
username = ...
password = ...
dialect = com.foo.bar.MyCustomMySQL5InnoDBDialect
}