Grails Spring Security手动触发onAuthenticationSuccessEvent

时间:2014-05-14 16:33:52

标签: spring grails

我有一些手动验证用户的代码,但我希望这也能触发onAuthenticationSuccessEvent中包含的逻辑。我现在的代码看起来像这样:

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

它可以很好地记录用户,但不会像我想象的那样触发onAuthenticationSuccessEvent。我认为对authenticationSuccessHandler.onAuthenticationSuccess的调用会触发这个,但它没有。如何在手动登录时触发此事件?

谢谢!

1 个答案:

答案 0 :(得分:5)

您始终可以通过DefaultAuthenticationEventPublisher要求发布身份验证成功事件。例如,在服务中你可以:

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

所有这些都记录在Spring Security API和Spring框架中。