mysql - Grails Spring Security UI MySQL 所有用户禁用?

标签 mysql grails spring-security

我将 Grails 2.1.1 与 Spring Security 1.2.7.1 和 Spring Security UI 0.2 一起使用。

当我使用内存数据库在 BootStrap.groovy 中创建一些测试用户时,我可以登录并管理用户 - 一切正常。但是,当我将数据源切换到 MySQL 数据库时,该服务指出当我尝试登录时该用户帐户已被禁用。我已经检查过,密码似乎已正确散列(即,它与数据库中的内容匹配) 。我对用户所做的唯一修改是包含一个 Study ID。在尝试调试代码时,我发现 org.codehaus.groovy.grails.plugins.springsecurity.GrailsUser 指示启用= 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

最佳答案

如果直接对数据库进行更改也会出现错误,并且它适用于“内存数据库”,我认为 MySQL 可能是问题所在。

MySQL 可能使用 Hibernate 无法正确使用的类型来存储值作为 boolean

您可以通过从数据库检索用户并手动检查来测试:

if( ! user.enabled ) println "not enabled"  // or something like that

最近 3 篇帖子 this mail list给出一个在 MySQL 中正确映射 boolean 的解决方案:

您可以使用自定义方言将 BIT(1) 更改为 bool 值:

   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 
   } 

关于mysql - Grails Spring Security UI MySQL 所有用户禁用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12999944/

相关文章:

mysql - UNION 与 HQL 中的排序依据

PHP mysql charset utf8问题

grails - 关于Grails GSP

grails - 有什么好的学习Grails的资源?

mysql - 如何在带有 Gorm 的 Grails 中使用 GROUP_CONCAT

c# - asp.net文件上传和文件读取(图片)

mysql - 在MySQL中创建基于组的自动增量列

grails - Grails Spring Websocket插件和Spring安全性

java - 不使用Spring ACL,实现基于实体 "creator"的方法级安全检查

java - Spring Security CSRF 支持手动安全配置