Grails 2 cannot log in using spring security when using multiple databases

In Grails 2.0.3, I installed Spring Security Core and created User, UserRole, and Role objects according to the tutorial: http://blog.springsource.org/2010/08/11/simplified-spring-security-with-grails/

Everything went fine until I decided to add a second data source in preparation for accessing objects from another database. DataSource.groovy is as follows:

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'
    }
}

Now I cannot log in - although all I did was add datasource_product. If I comment on this and recreate users (in Bootstrap.groovy), then I can log in again. Bootstrap.groovy contains:

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 )
}

Any ideas?

+3
source share
3

Gaaaahh. : 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, User:

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

    def beforeUpdate() {
        if (isDirty('password')) {
            isPasswordEncoded = false
            encodePassword()
        }
    }
....snip....
}
+6

, , . . , , , afterUpdate() reset .

    static transients = ['beforeInsertRunOnce','beforeUpdateRunOnce']
    boolean beforeInsertRunOnce
    boolean beforeUpdateRunOnce

    def beforeInsert() {
        if (! beforeInsertRunOnce) {
            beforeInsertRunOnce = true
            encodePassword()
        }
    }

    def afterInsert() {
        beforeInsertRunOnce = false
    }

    def beforeUpdate() {
        if (isDirty('password') && ! beforeUpdateRunOnce ) {
            beforeUpdateRunOnce = true
            encodePassword()
        }
    }

    def afterUpdate() {
        beforeUpdateRunOnce = false
    }
+3

I had a similar problem. Because I forgot to add

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

After that, authentication was performed.

0
source

All Articles