Using Spring Security Plugin in Grails

14 June 2011 12:37 AM

During my quest on using the Spring Security Plugin in Grails I’ve faced some difficulties.  I’m documenting what I’ve done here to future reference and hoping someone else find it useful.

1. Tutorial

Here’s a good tutorial about how to setup and start with the spring security plugin

http://blog.springsource.com/2010/08/11/simplified-spring-security-with-grails/

And the complete guide about the plugin

http://burtbeckwith.github.com/grails-spring-security-core/docs/manual/guide/single.html

2. Field or property  cannot be found on object of type ‘org.springframework.security.web.access.expression.WebSecurityExpressionRoot’  Caused by: Failed to evaluate expression.  Is ROLE_ prefix mandatory?

According to what I’ve read Spring Security uses two modes.  A traditional mode and a mode based on Spring Expression Language (SpEL).

http://burtbeckwith.github.com/grails-spring-security-core/docs/manual/guide/single.html#5.4%20Using%20Expressions%20to%20Create%20Descriptive,%20Fine-Grained%20Rules

I’m not sure if its possible to specify which mode the plugin will use,  as it’s done in Spring-Security

http://forum.springsource.org/showthread.php?78426-Failed-to-evaluate-expression-sec-intercept-url

But according to this post

http://permalink.gmane.org/gmane.comp.lang.groovy.grails.user/107548

and my experience,  if you intend to work using the traditional config, you have to name your authorities with the prefix “ROLE_”.   If you don’t do so the plugin will assume you’re working with SpEL and try to parse your authority name as an expression,  throwing an exception.

3. UI

I was thinking on how to create the views and controller to support user registration and administration,  when I found out there is a Spring-Security-UI plugin that does this for you.  Here’s an example on how to do this

http://www.objectpartners.com/2011/05/24/customizing-the-grails-spring-security-ui-plugin/

and the complete documentation

http://burtbeckwith.github.com/grails-spring-security-ui/docs/manual/index.html

4.  Installing Spring-Security-UI plugin.  Failed to install plugin [mail-1.0-SNAPSHOT]. Plugin has missing JAR dependencies.

The Spring-Security-UI plugin depends on the Mail plugin which I could not install in my first attempt,  because  it’s dependencies cannot be resolved using the default grails repositories.   So I had to uncomment the mavenCentral()  repository in BuildConfig.groovy,  uninstalled the previous plugin and reinstalled again.   I found the answer in this thread:

http://grails.1312388.n4.nabble.com/spring-security-ui-depending-on-mail-plugin-gt-dependency-resolution-fails-td3238433.html

5. Persistent cookies

To enable persistent cookie support you have to create a specific domain class as explained here

http://burtbeckwith.github.com/grails-spring-security-core/docs/manual/ref/Scripts/s2-create-persistent-token.html

 

That’s all for the moment.  I’ll continue working and updating this post :)

 

Read More

What I’ve learned about GORM

24 March 2011 11:09 PM

I’m starting to learn Grails and I’m working in a project where I have to connect an existing database to Grails by custom mapping. While working on this I’ve had some difficulties and questions, here’s a summary of what I’ve learned about them :)

Problem 1: Is there a tool that generates Groovy classes from existing tables?

Yes. It’s called GRAG and does a pretty decent job.  I still had to tweak somethings by hand,  but GRAG saved me a lot of time :D .

Problem 2: Is the id field mandatory in GORM?  How do I map a table in where the primary key is made of various fields and has no id of its own?

The identification field is mandatory.   Tables with composite keys can be mapped using the  ‘composite’  atributte in this way:

class Person {
  String firstName
  String lastName
  static mapping = {
      id composite:['firstName', 'lastName']
  }
}

http://grails.org/doc/latest/guide/5.%20Object%20Relational%20Mapping%20%28GORM%29.html#5.5.2.5%20Composite%20Primary%20Keys

Problem 3:  I didn’t realize ‘composite’ field’s  parameters were referring to  field names and not column names.

So in my first attempt to create the composite key I did this

class Person {
  String firstName
  String lastName
  static mapping = {
      id composite:['First', 'Last']
  }
}

Assumnig First and last were the names of the columns on the table. (Compare this with the section above)

http://grails.1312388.n4.nabble.com/DataSources-Plugin-with-Read-Only-Database-Unable-to-map-a-composite-key-No-property-found-for-name–td1391729.html

Problem 4:  Domain objects with composite keys must implement serializable

The compiler told me so XD

Problem 5:  ‘properties’  is a reserved word

I couldn’t give the name ‘properties’ to one of my fields.  Apparently it is a reserved word and cannot be used.

I wrote something like this:

class Item {
 
static hasMany = [properties: ItemProperties]
 
}

And the compiler gave me this error:

No such property: id for class: groovy.util.MapEntry

Googling I found this link

http://archive.codehaus.org/lists/org.codehaus.grails.user/msg/84b9b45c0806050258n118a1d66v82b9bdda4e4ed2b1@mail.gmail.com

and rewrote my code to:

class Item {
 
static hasMany = [itemProperties: ItemProperties]
 
}

which runs without problems :)

 

That’s all at the moment.  Hope someone would find this useful.

Read More