Grails3 spring security core 3 cannot use domain objects

Viewed 345

I am trying to create a grails 3.1.2 website with spring security core 3.0.4.

I have used this page: Grails Spring Security Core

I used the following scripts (replacing "website" for my project name):

grails s2-quickstart com.website User Role
grails s2-create-persistent-token com.website.PersistentLogin
grails s2-create-role-hierarchy-entry com.website.RoleHierarchyEntry

Then I added the following to my BootStrap.groovy:

if(User.count()==0) {
        Date testDate = new Date()

        Role userRole = new Role('ROLE_USER').save()
        Role adminRole = new Role('ROLE_ADMIN').save()

        User user = new User("John","password").save()
        User admin = new User("Richard","password").save()

        UserRole.create(admin, adminRole)
        UserRole.create(user, userRole)

        UserRole.withSession {
            it.flush()
            it.clear()
        }

        assert User.count() == 2
        assert Role.count() == 2
        assert UserRole.count() == 2

    }

I received an error about not being able to find authorities of the User bean class, so I replaced the getAuthorities method with this (as adding it to the transient list didn't work):

Set<Role> getAuthorities() {
    if(this.id){
        return UserRole.findAllBySecUser(this)*.role
    }
    else{
        return [] as Set
    }
}

Then I try and build again, and receive an exception about the UserRole class.

ERROR org.springframework.boot.SpringApplication - Application startup failed
org.codehaus.groovy.runtime.metaclass.MethodSelectionException: Could not find which method <init>() to invoke from this list:
  public com.website.UserRole#<init>(org.springsource.loaded.C)
  public com.website.UserRole#<init>(com.website.User, com.website.Role)
    at groovy.lang.MetaClassImpl.chooseMethodInternal(MetaClassImpl.java:3197) ~[groovy-2.4.6.jar:2.4.6]
    at groovy.lang.MetaClassImpl.chooseMethod(MetaClassImpl.java:3134) ~[groovy-2.4.6.jar:2.4.6]
    at groovy.lang.MetaClassImpl.createConstructorSite(MetaClassImpl.java:3434) ~[groovy-2.4.6.jar:2.4.6]
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.createCallConstructorSite(CallSiteArray.java:91) [groovy-2.4.6.jar:2.4.6]
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:60) [groovy-2.4.6.jar:2.4.6]
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:235) [groovy-2.4.6.jar:2.4.6]
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:247) [groovy-2.4.6.jar:2.4.6]
    at com.website.UserRole.create(UserRole.groovy:55) ~[main/:na]

How do I get past his error? I have no idea what this particular error means :/

1 Answers
Related