Grails 2使用多个数据库时无法使用spring security登录

2024-02-21

在 Grails 2.0.3 上,我安装了 Spring Security Core 并按照教程创建了 User、UserRole 和 Role 对象:http://blog.springsource.org/2010/08/11/simplified-spring-security-with-grails/ http://blog.springsource.org/2010/08/11/simplified-spring-security-with-grails/

一切都很顺利,直到我决定添加第二个数据源以准备从不同的数据库访问对象。 DataSource.groovy 看起来像这样:

test {
    dataSource_product {
        dbCreate = "update"
        url = "jdbc:mysql://localhost/products"
        pooled = true
        driverClassName = "com.mysql.jdbc.Driver"
        username = "blah"
        password = "blah"
        loggingSql = true
        dialect = 'org.hibernate.dialect.MySQL5InnoDBDialect'
    }
    dataSource {
        dbCreate = "update"
        url = "jdbc:mysql://localhost/core"
        pooled = true
        driverClassName = "com.mysql.jdbc.Driver"
        username = "blah"
        password = "blah"
        loggingSql = true
        dialect = 'org.hibernate.dialect.MySQL5InnoDBDialect'
    }
}

现在我无法登录 - 尽管我所做的只是添加 datasource_product。如果我将其注释掉并重新创建用户(在 Bootstrap.groovy 中),那么我可以再次登录。 Bootstrap.groovy 包含:

def init =
{ servletContext ->

    // Add in roles
    Role.withTransaction { 
        def adminRole = Role.findByAuthority ( Role.ROLE_ADMIN ) ?: new Role ( authority: Role.ROLE_ADMIN ).save ( failOnError: true )

        def adminUser = User.findByUsername ( 'admin' ) ?: new User (
                username: 'blah',
                password: 'blah',
                enabled: true ).save ( failOnError: true )
        if ( !adminUser.authorities.contains ( adminRole ) ) UserRole.create ( adminUser, adminRole )
}

有任何想法吗?


嘎啊啊。发现这个:http://jira.grails.org/browse/GRAILS-8237 http://jira.grails.org/browse/GRAILS-8237- 显然,beforeInsert 会在每个数据源的每个域上被调用。这意味着,在我的 User 对象中,encodePassword 被调用两次 - 我对密码进行了双重编码:

def beforeInsert() {
    encodePassword()
}

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

protected void encodePassword() {
    password = springSecurityService.encodePassword(password)
}

我在 JIRA 中看到了一个补丁,但在它发布之前,我使用 isPasswordEncoded 标志创建了一个解决方法,以防止用户中的多个编码:

class User {
    boolean isPasswordEncoded = false
....snip....
    def beforeInsert() {
        if ( !isPasswordEncoded )
        {
            isPasswordEncoded = true
            encodePassword ()
        }
    }

    def beforeUpdate() {
        if (isDirty('password')) {
            isPasswordEncoded = false
            encodePassword()
        }
    }
....snip....
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Grails 2使用多个数据库时无法使用spring security登录 的相关文章

随机推荐