Question

I have some code that is manually authenticating a user, but I want this to also trigger the logic contained in the onAuthenticationSuccessEvent. The code that I have right now looks like this:

springSecurityService.reauthenticate(user.username)
authenticationSuccessHandler.onAuthenticationSuccess(request, response, springSecurityService.getAuthentication())

It logs the user in fine, but does not trigger the onAuthenticationSuccessEvent like I thought it would. I figured the call to authenticationSuccessHandler.onAuthenticationSuccess would trigger this, but it doesn't. How can I get this event to trigger while logging in manually?

Thanks!

Was it helpful?

Solution

You can always ask that an authentication successful event through the DefaultAuthenticationEventPublisher be published. For example in a service you could:

class MyService {
  def authenticationEventPublisher
  def springSecurityService
  ...
  void someMethod() {
    ...
    authenticationEventPublisher.publishAuthenticationSuccess(
      springSecurityService.getAuthentication()
    )
    ...
  }
  ...
}

It's all documented in the Spring Security API and Spring framework.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top