Moving to Grails

Spring security core with groups

In this blog post I will look into assigning roles through groups.

Group assignment becomes more interesting with:

  • increasing user count
  • increasing role count
  • users with similar access needs


There are three basic domains when configuring spring security for groups.

  • User
  • Role
  • RoleGroup

We also have these many-to-many helper domains:

Each user can be member of any rolegroup.

And each rolegroup can have any role.

This setup means that roles are assigned to rolegroups. No roles are assigned directly to users.

Create the application

grails create-app ssc-groups1 --profile=web

Add spring security to build.gradle:

compile 'org.grails.plugins:spring-security-core:4.0.0'

Initialize spring security:

grails s2-quickstart User Role --groupClassName=RoleGroup

Initialize user, role, group


package ssc.groups1


class BootStrap {

    def init = { servletContext ->
        def adminUser = User.findByUsername('admin')
        if (!adminUser) {
            User.withNewTransaction {
                adminUser = new User(username: 'admin', password: 'password')
                        .save(flush: true)
  "Added user admin: ${adminUser}")

        def adminsRoleGroup = RoleGroup.findByName('Admins')
        if (!adminsRoleGroup) {
            RoleGroup.withNewTransaction {
                adminsRoleGroup = new RoleGroup(name: 'Admins')
                        .save(flush: true)
  "Added role-group Admins: ${adminsRoleGroup}")

        def adminRole = Role.findByAuthority('ROLE_ADMIN')
        if (!adminRole) {
            Role.withNewTransaction {
                adminRole = new Role(authority: 'ROLE_ADMIN')
  "Added role ROLE_ADMIN: ${adminRole}")

        def adminsToAdminRoleGroup = RoleGroupRole.get(,
        if (!adminsToAdminRoleGroup) {
            RoleGroupRole.withNewTransaction {
                adminsToAdminRoleGroup = new RoleGroupRole(roleGroup: adminsRoleGroup, role: adminRole)
                        .save(flush: true)
  "Added admin role to admins role-group -> adminsToAdminRoleGroup: ${adminsToAdminRoleGroup}")

        def adminsUserRoleGroup = UserRoleGroup.get(,
        if (!adminsUserRoleGroup) {
            UserRoleGroup.withNewTransaction {
                adminsUserRoleGroup = new UserRoleGroup(user: adminUser, roleGroup: adminsRoleGroup)
                        .save(flush: true)
  "added admin to admins-role-group -> adminsUserRoleGroup: ${adminsUserRoleGroup}")
    def destroy = {

Add user UI

grails generate-all

Add @Secured to UserController

class UserController {

Modify application.groovy:

grails.plugin.springsecurity.logout.postOnly = false

The complete application is here:


In 4 blog posts I have covered different sides of configuring spring security plugin.

Parts of our applications can store business critical and privacy information. When starting up with Grails the plan was to enable some kind of two-factor authentication in order to protect data better than password only.

It was only after attending an ObjectComputing webinar I was made aware that they had successfully configured the Spring security domains for multitenancy.

Finally in this post touched groups, so that most users can be configured with one or just a few group memberships.

Play with spring security in more ways than you need.