我想创建CustomAuthenticationSuccessHandler
将日志添加到每个用户登录名:
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。
最佳答案
您本质上做错了。
您应该注册一个事件侦听器,而不是覆盖成功处理程序。
配置:grails.plugin.springsecurity.useSecurityEventListener = true
将类(class)注册为Bean
class MySecurityEventListener
implements ApplicationListener<InteractiveAuthenticationSuccessEvent> {
@Autowired
UserService userService
void onApplicationEvent(InteractiveAuthenticationSuccessEvent event) {
userService.postLoginMessage(event.authentication.principal.username, null)
}
}
关于spring - 创建自定义AuthenticationSuccessHandler并在完成后调用默认的AuthenticationSuccessHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45963740/