Monday, November 2, 2009

Grails, Shiro, addToPermissions() error

I've been playing with Grails with an eye to using it on a new project.

Apache Shiro looks like a suitable security framework for me so I thought I'd use it right from the off. There is even a plugin for Grails to simplify installation.

I discovered that you still have to create your own shiro.jar from the shiro source - but that was relatively straight forward....

[... once I had downloaded a new version of Maven (Fedora 11 packaged version wasn't recent enough)....]

So, once I had installed shiro.jar, and the plugin, I decided to run:
'grails create-db-realm' and
'grails create-authcontroller'
and added bootstrap code according to the plugin advice

ASIDE: for the purposes of just playing around. A better choice for 'playing around' is to do:
'grails quick-start'


If you followed the same route as me, you may have experienced:

Caused by: groovy.lang.MissingMethodException: No signature of method: static groovy.lang.MissingMethodException.addToPermissions() is applicable for argument types: (java.lang.String) values: [*:*]

The fix I arrived at is to add the line:
static hasMany = [ roles: ShiroRole, permissions: String ]
to ShiroUser.groovy so it becomes:

class ShiroUser {
String username
String passwordHash

static hasMany = [ roles: ShiroRole, permissions: String ]

static constraints = {
username(nullable: false, blank: false)
}
}


This got me past this error, however since I feel that I still don't properly 'get it', I expect more issues (and possibably blog posts) in the future!

1 comment:

Unknown said...

For reason not yet known to me, it seems that the quick-start script didn't create the WildcardRealm as it's supposed to be the default, but instead the script created the ShiroDbRealm. The WildcardRealm will create domain classes WildcardUser and WildcardRole which already has the "roles" relationship defined (Look in the src/templates). The ShiroDbRealm will create additional domain classes to handle the user, role, and permission relationships. I'm still don't quite get it either, like what's the difference between the WildcardRealm and ShiroDbRealm?