Skip to content


Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Latest commit


Git stats


Failed to load latest commit information.


Build Status

Grails plugin to implement really stateless authentication. It is based on the procedure described at

Long story short: we do not keep tokens. We generate a self-contained token, sign it, and give it to the client. The client then send this token on every request. As the token is signed, the client can't mess with it. And as the token is self-contained, we don't need to keep it, only verify it.

In general, when you try to access a protected method, you will get a 401 (UNAUTHORIZED) if there isn't a valid token on the Authorization header.



The plugin can be used on a pure stand alone mode. For this, you only need to set two parameters on Config.groovy: = "mysupersecretkey" = false

Secure controllers and methods

You can mark any controller or method with @SecuredStateless, and the plugin will keep the authentication for you, adding to the request an attribute 'securityStatelessMap' with the user data.


class HelloController {
    def index() {
        render "hello ${request.securityStatelessMap.username}"


On standalone mode, you have to code your own login controller. This controller should validate the credentials as you need, and return a security-stateless token, using the method statelessTokenProvider.generateToken(String username).

A silly example of login controller could be:

import grails.converters.JSON

class LoginController {
    def statelessTokenProvider

    def index() {
        if (params.password == '12345'){
            render (['token': statelessTokenProvider.generateToken(params.user)] as JSON)
        } else {
            render(status: 401, text: '')

Integrated with springsecurity


The plugin can be used along with springsecurity. In order to do so, you need to set several parameters on Config.groovy: = "mysupersecretkey" = true

Also, you need to add the statelessAuthenticationFilter to the list of filters of your application adding this line to BootStrap.groovy

SpringSecurityUtils.clientRegisterFilter('statelessAuthenticationFilter', SecurityFilterPosition.SECURITY_CONTEXT_FILTER.order + 10)

Secure controllers and methods

Any method controlled by the defined chainMap is secured. And inside it, you can access both springSecurityService.currentUser and request.securityStatelessMap.

import grails.plugin.springsecurity.annotation.Secured

class HelloController {
    def springSecurityService

    def index() {
        render "hello ${springSecurityService.currentUser.username} ${request.securityStatelessMap.username}"


You can code your own login controller. But the plugin offers you a default method for login. In order to use it, you only need to configure several parameters on Config.groovy. First, you need to add and endpoint to the chainMap. Next, define the follow parameters:

  • true for activate login
  • the same login url defined on chainMap
  • The name of the parameter for the username. By default it is "user".
  • The name of the parameter for the username. By default it is "password". = "mysupersecretkey" //as allways = true
grails.plugin.springsecurity.filterChain.chainMap = [
    '/stateless/login': 'statelessLoginFilter'
] = true = "/stateless/login" = "user" = "password"

The login will return 400 (BAD_REQUEST) if there isn't username or password, 401 (UNAUTHORIZED) for wrong username/password, or 201 (CREATED) for valid username/password. On 201, also return a JSON body with the token:

Invalidation on log in

In certain security scenarios we want to invalidate the user when another user logs into the platform. Activating the following flag the salt will be renewed invalidating all of previous tokens when the user logs in. = true

Token format

Currently the plugin supports two token formats:

Legacy format (default)

Encrypted internal format. This format is recommended when your extraData field could have sensitive data.

Custom format for internal representation of the token, encoded an encrypted using PBKDF2 (Password-Based Key Derivation Function 2).

This is the default configuration but if you want to explicitely activate it you can set it on the Config.groovy file: = "Legacy"
JWT format

Standarized format using the format defined in the JWT specification. Uses the HS256 as the implementations algorithm

You can activate this format on your Config.groovy file as follows: = "JWT"

Additional configuration

Expiration time

By default stateless tokens are configured so they don't expire. You can add an expiration time so your tokens expires after the indicated minutes. = 1440 // 1 day
Expiration response code

If you have configured the expiration time you can additionaly configure the response code that will be retrieved when a token expires.

By default it's a 401 Unauthorized status but you can choose another like 419 or 498 = 401


Grails plugin to implement stateless authentication using Spring Security







No packages published

Contributors 4