0

I am new to Groovy and Grails. I have developed an application using the Spring Security plugin using a database requested request map. I want a custom redirection to the home pages after logout according to the roles.

If the user is ROLE_ADMIN, after logout he would be redirected to his home page in views adminUser/Homepage.gsp

If the user is ROLE_USER, after logout he would be redirected to his home page in views User/Homepage.gsp

I am not able to get any custom authentication redirection according to the user role.

Burt Beckwith
  • 75,342
  • 5
  • 143
  • 156
Abhishek
  • 452
  • 3
  • 19
  • I guess you will have to create a custom logout method were you can logout user programmatically using `SecurityContextHolder.clearContext()` then redirect user based on there ROLES – Anant Kolvankar Jun 03 '15 at 07:25
  • There are options to create custom logout handler as well you can follow this link https://grails-plugins.github.io/grails-spring-security-core/guide/logoutHandlers.html – Anant Kolvankar Jun 03 '15 at 07:32
  • @AnantKolvankar - http://stackoverflow.com/questions/5727380/how-to-manually-log-out-a-user-with-spring-security this solved me... thanks for helping me out – Abhishek Jun 03 '15 at 09:35
  • I have posted an answer If you think it's helpful please accept :) – Anant Kolvankar Jun 03 '15 at 12:01

2 Answers2

0

You have two options

1) Create a custom logout method were you can logout user programmatically using SecurityContextHolder.clearContext() then redirect user based on there ROLES

2) Create custom logout handler, follow this link https://grails-plugins.github.io/grails-spring-security-core/guide/logoutHandlers.html

Anant Kolvankar
  • 1,050
  • 9
  • 10
0

I am posting my solution thanks a lot for your help. This approach is simple and easy you just need to call the logout method from your request variable from your logout controller.

def roles = SpringSecurityUtils.getPrincipalAuthorities()

        for (String role in roles) {
            if (role.equals("ROLE_ADMIN")) {
                request.logout()
                redirect uri : "/admin/logoutPage"
            }
            else if (role.equals("ROLE_USER")) {
                request.logout()
                redirect uri : "/user/logoutPage"
            }
            else {
                request.logout()
                redirect uri : "/"
            }
        }
Abhishek
  • 452
  • 3
  • 19