spring - 创建自定义AuthenticationSuccessHandler并在完成后调用默认的AuthenticationSuccessHandler

标签 spring spring-mvc grails groovy

我想创建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/

相关文章:

json - Grails - JSONBuilder - 规范 toPrettyString() 返回 stackoverflow

unit-testing - 在grails中的taglib的单元测试期间,属性为空

csv - 将大量数据从CSV文件上传到数据库的有效方法?

java - 通过hibernate从数据库读取数据

eclipse - 使用 Spring 框架的 Eclipse 中的 Java Web App 无法识别对 Controller 的编辑

java - 我们如何将 dojo EnhancedGrid Store 项目传递给 spring MVC Controller 并保存在 DB 中?

java - 服务器到客户端的响应状态

java - RequestMapping 类级别和方法级别之间的继承

java - 如何结合 Jersey、JSON 和 Spring

java - 简单的 Spring Boot 应用程序的 ResourceLoader null