Thursday, June 6, 2013

Using Shibboleth Security with Grails

This post discusses setting up Grails to be able to access a user ID provided by Shibboleth via request headers. It does not cover base setup of the Spring Security plugin for Grails or of Shibboleth and Apache. I assume you have all that set up. If your Shib installation is providing data via request attributes rather than headers, you may be able to use the Shib plugin for Grails instead. (If I have some free time someday I may look into trying to modify that plugin with an option to read Shib data from headers).

Basically, you will use Spring's RequestHeaderAuthenticationFilter to get the user ID from the request headers and PreAuthenticatedAuthenticationProvider which is for use with any system where the user has already been authenticated (in this case by Shibboleth). This example also makes use of a custom MockRequestHeaderAuthenticationFilter which allows you to pass in a user ID when you're in the development environment to make testing easier.

Of course, first thing is you need to install Spring Security plugin and set its properties so it knows about your domain classes. Then, in conf/spring/resources.groovy you will need to set up a few beans:

In conf/Config.groovy, you need to let Spring Security know to use your preauthAuthenticationProvider:
grails.plugins.springsecurity.providerNames = ['preauthAuthenticationProvider']

This block in conf/BootStrap.groovy actually registers the filter for each environment. For development and test we want the mock filter and for QA and production, we use the real filter:

This is the entirety of the MockRequestHeaderAuthenticationFilter.groovy class. It just returns whatever user name you told it to in the spring bean setup above:

1 comment:

  1. Thanks Ed, that was very helpful so thanks for writing this up.
    I've also documented my own version on my blog for others interested in seeing various solutions to the same problem

    http://pwu-developer.blogspot.com.au/2015/12/grails-2x-and-shibboleth-authentication.html

    Cheers

    ReplyDelete