Spring Security Permission Based framework

28 / Apr / 2014 by Mohit Garg 2 comments

In my recent project, I want to implement permission based framework with spring security grails plugin.

What does permission based framework mean?

We can create a ROLE at run time based on the permission given to the user. Most of the times, we use Spring security in a way where we pre-define the roles to be used in our apps. But with the use of Permission, we can define the role at run time and assign permission as per our requirement.

Let’s take an example : Suppose we have a controller named as PersonController

class PersonController {


def create(){….}


def edit(){…}


def show(){….}

def delete(){….}


In this example, we have defined four action, We want to create 2 roles like this :-

  • Edit Person: This role has permissions like edit, create, delete, show.
  • Show Person : This role has permissions like show.

So, for this, we have to define the different permissions to each action or may be you can define same permission to different action as same as we are defining roles in our projects.

After that, we have to add a new domain named as Permission, we have to define the many to many relationship with Role domain.


class Role implements Serializable {

String authority
Date dateCreated
Date lastUpdated

static hasMany = [permissions:Permission]

static mapping = {
cache true

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


class Permission implements Serializable {

String name
static constraints = {

In Bootstrap.groovy, you can bootstrap roles and permissions. See the code snippet below:

Firstly you have to save different type of permissions that mentioned above the action name in @secured annotation and assign permission to each role as per your requirements.

Permission createPermission = new Permission(name: ‘PERSON_CREATE’)

Permission editPermission = new Permission(name: ‘PERSON_EDIT’)

Permission deletePermission = new Permission(name: ‘PERSON_DELETE’)

Permission showPermission = new Permission(name: ‘PERSON_SHOW’)

Role editRole = new Role(authority: ‘ROLE_EDIT_PERSON’, permissions: [createPermission, deletePermission, showPermission, editPermission])

Role showRole = new Role(authority: ‘ROLE_SHOW_PERSON’, permissions: [showPermission])

You can create as many as can roles at run time and assign permissions to each role.

You have to create one class in src/groovy package CustomAuthority which will extends the GrantedAuthority

import org.springframework.security.core.GrantedAuthority

class CustomAuthority implements GrantedAuthority {
private String authority;

public CustomAuthority(String authority) {
this.authority = authority;

public String getAuthority() {
return authority;

public int hashCode() {
return authority.hashCode();

public boolean equals(Object obj) {
if(obj == null) return false;
if(!(obj instanceof CustomAuthority)) return false;
return ((CustomAuthority) obj).getAuthority().equals(authority);

Now, you have to override getAuthorities() method of the User.groovy file to use the permission for each role specific to users.

Set getAuthorities() {
Set list = UserRole.findAllByUser(this).collect { it.role } as Set
def roles = new HashSet()
def newRoles = new HashSet()
list.each { role ->
role?.permissions?.each { permission ->
CustomAuthority customAuthority = new CustomAuthority("ROLE_PERMISSION_" + permission.name);
return newRoles

In this method, we are getting the different permissions from each role and assigning them to authority list.

Now, your permission based framework is configured.

It’s very powerful way to customize the roles in spring security, you don’t need to change the code when creating the role, you can customize roles based on permissions at run time.

Hope this blog will give you the idea to integrate the spring security with permissions.


comments (2)

  1. Mohit

    Hi Ricardo,

    This problem is related to the org.springframework.security:spring-security-web:jar:3.2.0.RC1 jar file, Using grails 2.3.8 with spring-security-core:2.0-RC2, It’s not able to download the org.springframework.security:spring-security-web:jar:3.2.0.RC1 jar file from maven repository.

    Add following line in BuildConfig.groovy file:
    repositories {
    mavenRepo ‘http://repo.spring.io/milestone’

    It’ll download org.springframework.security:spring-security-web:jar:3.2.0.RC1 this jar file and from this you will get the GrantAuthority class.

    If you are still facing any problem, please let me know.


  2. Ricardo

    Good Article, thanks!

    I have a problem when trying to implement the CustomAuthority. It looks like my environment do not have a import org.springframework.security.core.GrantedAuthority.

    Do you have any idea what the problem might be?
    I’m using grails 2.3.8 and spring-security-core:2.0-RC2


Leave a Reply

Your email address will not be published. Required fields are marked *