Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

handle successful login event with spring security

My Grails app uses the Spring Security plugin. Whenever a user successfully logs in I want to:

  • store something in the session
  • redirect them to a custom page (depending on their role)

I need to handle logout events similarly, which was pretty straightforward because the plugin provides a bean named logoutSuccessHandler that can be overriden. I was hoping to similarly find a bean named loginSuccessHandler, but no such luck.

I read the page in the plugin's docs about event handling, but neither of the event handling mechanisms appears to give me access to the current request or session.

like image 846
Dónal Avatar asked Feb 14 '23 07:02

Dónal


1 Answers

If you want to do some stuff upon successful login. You can listen to InteractiveAuthenticationSuccessEvent

class AuthenticationSuccessEventListener implements    
                       ApplicationListener<InteractiveAuthenticationSuccessEvent> {


    @Override
    public void onApplicationEvent(InteractiveAuthenticationSuccessEvent event) {
         .......do some stuff here
    }
   }

And then register AuthenticationSuccessEventListener as a spring bean in resources.groovy You can do whatever you want here, however you wont be able to do redirect from listener.

Here's another similar question

like image 75
Sudhir N Avatar answered Mar 23 '23 14:03

Sudhir N