2017-08-30 123 views
0

當我想創建CustomAuthenticationSuccessHandler添加日誌,每個用戶登錄調用默認AuthenticationSuccessHandler:創建自定義AuthenticationSuccessHandler,做

class CustomAuthenticationSuccessHandler implements AuthenticationSuccessHandler{ 

    UserService getUserService() { 
     return ApplicationContextHolder.getBean('userService') 
    } 

    @Override 
    public void onAuthenticationSuccess(HttpServletRequest req, 
      HttpServletResponse res, Authentication auth) throws IOException, 
      ServletException { 
     userService.postLoginMessage(auth.principal.username, null) 

    } 
} 

這是很簡單的代碼,並且效果很好,但現在我有沒有返回值。有沒有辦法在自定義代碼運行後返回默認值AuthenticationSuccessHandler或返回默認值?

我的意思是返回Authentication json。

回答

1

你基本上做錯了。

而不是重寫成功處理程序,您應該註冊一個事件偵聽器。

配置: grails.plugin.springsecurity.useSecurityEventListener = true

註冊類作爲一個bean

class MySecurityEventListener 
     implements ApplicationListener<InteractiveAuthenticationSuccessEvent> { 

    @Autowired 
    UserService userService 

    void onApplicationEvent(InteractiveAuthenticationSuccessEvent event) { 
userService.postLoginMessage(event.authentication.principal.username, null) 
    } 
}