Pablo Mosby
Pablo Mosby

Reputation: 319

Spring Security - Grails 3.2.2 - ERR_TOO_MANY_REDIRECTS

I have this on my application.yml:

grails.plugin.springsecurity.successHandler.alwaysUseDefault = true
grails.plugin.springsecurity.successHandler.defaultTargetUrl = '/l/loggedIn'

And the l/loggedIn looks like:

def loggedIn() {
    User user = springSecurityService.currentUser

    def roleDefault = Role.findByAuthority("ROLE_DEFAULT")
    if(user.authorities.contains(roleDefault))
        redirect(controller: 'foo', action:'index')

    def roleAdmin = Role.findByAuthority("ROLE_ADMIN")
    if(user.authorities.contains(roleAdmin))
        redirect(view: 'index')

}

When I log in as adminin my application I get the ERR_TOO_MANY_REDIRECTS error.

Any way to fix this?


Update

@Secured(["ROLE_ADMIN", "ROLE_DEFAULT"])
class FooController {

    def index() {}
}

Upvotes: 2

Views: 410

Answers (1)

Adeel Ansari
Adeel Ansari

Reputation: 39907

redirect doesn't take view in to account, render does.

Allowed options are, action, controller, uri, url, params, fragment, and/or a domain instance.

So, you can do something like,

def loggedIn() {
    User user = springSecurityService.currentUser

    def roleDefault = Role.findByAuthority("ROLE_DEFAULT")
    if(user.authorities.contains(roleDefault))
        redirect(controller: 'foo', action:'index')

    def roleAdmin = Role.findByAuthority("ROLE_ADMIN")
    if(user.authorities.contains(roleAdmin))
        redirect(controller: 'foo', action:'adminIndex')
}

then in your controller define both actions, with proper views available.

@Secured(["ROLE_ADMIN", "ROLE_DEFAULT"])
class FooController {

    def index() {}

    def adminIndex() {}

}

Read the docs.

Upvotes: 1

Related Questions