4
votes

So I am banging my head against the wall trying to get spring-security-core-1.2.7.1 to work with Grails 2.0...

I have looked at the tutorial and run s2. Read that the new plugin encrypts passwords for you, so my bootstrap looks like:

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
    }

I can look at the H2 database and I see the users, their encoded passwords, see that the roles are created and can see the user role mappings are properly created as well.

However, I still get "Sorry, we were not able to find a user with that username and password." at the login prompt for both users.

I have turned on log4j debug 'org.springframework.security' but all I really get out of the logs is:

2012-01-23 23:08:44,875 ["http-bio-8080"-exec-5] DEBUG dao.DaoAuthenticationProvider  - Authentication failed: password does not match stored value
4
flushing might help sometimes, u should try that! - Amanuel Nega

4 Answers

2
votes

I can't see anything obviously wrong with your code. I'm using the same version of Grails and the spring security plugin, and the following code in Bootstrap.groovy works for me:

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()
}
1
votes

I did have similar issue. Was because I've forgotten 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 working.

0
votes

You can fix the multiple datasources issue by updating the User class. See https://stackoverflow.com/q/13296594

-3
votes

In the OP's original code, why is the username in single quotes and the password in double quotes. That might be the problem.