Domanda

I have a grails application. I've installed spring security core plugin 2.0 RC2 and I'm creating roles, users and requestmap in BootStrap.groovy Grails is throwing the following error when I run the app.

ERROR context.GrailsContextLoader  - Error initializing the application: Unknown entity: Role

Here is my Role class (generated by the spring security plugin and modified to extend AbstractActivity with is an abstract class that has beforeInsert() method/closure and does some validation).

package com.mypackage

class Role extends AbstractDomain {

    String authority

    Date dateCreated
    Date lastUpdated
    User createdBy
    User updatedBy

    static mapping = {
        cache true
    }

    static constraints = {
        authority blank: false, unique: true

            createdBy nullable: true
            updatedBy nullable: true
    }
}    

AbstractDomain.groovy

package com.mypackage

import com.mypackage.User

class AbstractDomain implements Serializable {

transient springSecurityService

def beforeInsert() {

    if(null != springSecurityService) {

        User user = springSecurityService.getCurrentUser()

        if(null != user) {

            this.createdBy = user
        }
    }
}

}

BootStrap.groovy

import com.mypackage.Role

class BootStrap {
    def init = { servletContext ->
          if (Role.findByAuthority('ROLE_ADMIN') == null)
                def adminRole = new Role(authority: 'ROLE_ADMIN').save(flush: true)
    }
    def destroy = {}
}

Config.groovy

grails.plugin.springsecurity.userLookup.userDomainClassName ='com.mypackage.User'
grails.plugin.springsecurity.userLookup.authorityJoinClassName 'com.mypackage.UserRole'
grails.plugin.springsecurity.authority.className = 'com.mypackage.Role'
grails.plugin.springsecurity.requestMap.className = 'com.mypackage.Requestmap'
grails.plugin.springsecurity.securityConfigType = 'Requestmap'

How do I solve the Unknown entity issue?

È stato utile?

Soluzione

I found the solution! My AbstractDomain Class which was extended by Role class was in 'src/groovy' folder and I forgot to make it 'abstract'. Then I added the abstract keyword before class, it works like magic. Thanks @mgg for giving me the idea here

So, my AbstractDomain class now looks like

abstract class AbstractDomain implements Serializable {
...

Altri suggerimenti

You should have following configuration in Config.groovy to let Spring security plugin know which class you use for roles persistence:

  grails.plugins.springsecurity.authority.className = "com.mypackage.model.Role"

the same applies for users:

  grails.plugins.springsecurity.userLookup.userDomainClassName = "com.mypackage.model.User"

See http://grails-plugins.github.io/grails-spring-security-core/docs/manual/guide/domainClasses.html#authorityClass

Try replacing

import com.mypackage.Role

with

import com.mypackage.model.Role

This doesn't appear to be a Spring Security issue. The error is coming from Grails - "Unknown entity: Role". This usually happens when there is a mismatch between the package name in a domain class (or other artifact) and the folder the file is in.

If you want the Role class to be in the com.mypackage package, make sure that the file is in grails-app/domain/com/mypackage/Role.groovy. The names are case-sensitive too, so make sure that the folder and package names agree there too. Also, you probably didn't do this, but I've seen people make mistakes like creating a "com.mypackage" directory, putting the file in grails-app/domain/com.mypackage/Role.groovy. Any of these problems will cause the mismatch that you're seeing.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top