user1854751
user1854751

Reputation: 107

Using SpringSecurityCore bootstrap

just recently started programming in Groovy and already stuck. I am trying to create users available to login in my bootstrap, I have seen numerous tutorials and despite copying and pasting code I was greeted by numerous errors. I have now gotten down to code that seems to run but the user simply isn't there.

What am I doing wrong?

import grails.timesecurity.*
import timetracker2.*

class BootStrap {
    def springSecurityService

    def init = { servletContext ->

        def examples = [
            'rob' : [username: 'rob', password: 'password']
        ]

        def userRole = Authority.findByAuthority("ROLE_USER") ?: new Authority(authority: "ROLE_USER").save()
        def adminRole = Authority.findByAuthority("ROLE_ADMIN") ?: new Authority(authority: "ROLE_ADMIN").save()

        if(!Person.count())
        {
            userRole = new Authority(authority: 'ROLE_USER').save()

            //String password = springSecurityService.encodePassword('password')

            def user = new Person(
                username: "Rob",
                password: springSecurityService.encodePassword("Password"),
                enabled: true
                )
            PersonAuthority.create user, userRole
            //def user = new Person['Rob', password, enabled: true].save()
        }       
    }

    def destroy = {}
}

Anyone who can help is a legend!

Upvotes: 2

Views: 170

Answers (2)

Burt Beckwith
Burt Beckwith

Reputation: 75681

You don't call save() on the Person instance. Once that's fixed though, you won't be able to log in because you're following an old tutorial or blog post and you're explicitly encoding the password. But the generated Person class does this already, so it will be double-encoded. And to further confuse things, you're creating a second Authority with ROLE_USER.

Try this:

def userRole = Authority.findByAuthority("ROLE_USER") ?: new Authority(authority: "ROLE_USER").save()
def adminRole = Authority.findByAuthority("ROLE_ADMIN") ?: new Authority(authority: "ROLE_ADMIN").save()

if (!Person.count()) {

   def user = new Person(
      username: "Rob",
      password: "Password",
      enabled: true).save()

   PersonAuthority.create user, userRole
}

Upvotes: 4

doelleri
doelleri

Reputation: 19682

First, I'd wager you need to call save() on your new Person. After that, make sure your Person object is validating.

Upvotes: 0

Related Questions