Programmatically log out a user

grails, spring-security

Solution

class AnyController {    
    def logoutHandlers

    def method = {
    ...
        logoutHandlers.each { handler ->
            handler.logout(request, response, springSecurityService.authentication)
        }
    }
}

Problem

Upon user login I check their roles. If they have no roles I want to log them out. This is what I am doing now: ``` if (SpringSecurityUtils.ifAllGranted('ROLE_ADMIN')) { redirect(uri: '/book/edit') } else if (SpringSecurityUtils.ifAllGranted('ROLE_USER')) { redirect(uri: '/book/list') } else { redirect(uri: '/login/auth') } ``` This doesn't work because there is a recursive redirect to `/login/auth`. How do I do this properly (logout and then redirect to `/login/auth`)?

Original source