【问题标题】:Cleanest way to call multiple AuthenticationSuccessHandlers?调用多个 AuthenticationSuccessHandler 的最简洁方法?
【发布时间】:2013-08-10 18:28:03
【问题描述】:

我有一个使用 Spring 3.1 的 java webapp。我的 Spring 安全上下文定义了多个身份验证过滤器,每个过滤器对应一个不同的身份验证路径(例如,用户名/密码与单点登录)。每个身份验证过滤器都定义了自己的AuthenticationSuccessHandler。现在,我想注入 2 个额外的操作以在成功的身份验证时采取,它们应该适用于 所有 身份验证类型:

  1. 设置一个跟踪事件代码供 Google Analytics 在前端使用
  2. 在我们的数据库中更新用户的首选语言环境

在用户成功通过身份验证后,这些可能是您想要挂钩的任何操作。重要的一点是,与常规的 AuthenticationSuccessHandlers(每个身份验证路径不同)不同,它们不会转发或重定向请求。所以调用一堆是安全的。

有没有一种简洁的方法来集成这些额外的身份验证成功“操作”,使用 Spring Web/Security 3.1?

我考虑实现ApplicationListener<AuthenticationSuccessEvent>,但我的事件需要访问请求,而AuthenticationSuccessEvent 提供的只是Authentication 对象本身。

我找不到方法,所以我决定推出自己的代理:

public class AuthenticationSuccessHandlerProxy implements AuthenticationSuccessHandler {
    private List<AuthenticationSuccessHandler> authenticationSuccessHandlers;

    public AuthenticationSuccessHandlerProxy(List<AuthenticationSuccessHandler> successHandlers) {
        this.authenticationSuccessHandlers = successHandlers;
    }

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request,
                                        HttpServletResponse response,
                                        Authentication authentication) throws IOException, ServletException {
        for (AuthenticationSuccessHandler successHandler : this.authenticationSuccessHandlers) {
            successHandler.onAuthenticationSuccess(request, response, authentication);
        }
    }
}

【问题讨论】:

    标签: java spring spring-mvc spring-security


    【解决方案1】:

    在简要查看AbstractAuthenticationProcessingFilter 的源代码以及所有其他调用AuthenticationSuccessHandler.onAuthenticationSuccess(...) 的地方后,我认为使用 Spring Security 没有任何可能。

    作为一种解决方法,您可以尝试将成功处理程序包装到某个 AspectJ 或 AOP 切入点中,然后将此切入点应用于 AuthenticationSuccessHandler.onAuthenticationSuccess(...) 执行。也许像这样您可以针对所有身份验证类型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-28
      • 1970-01-01
      • 2021-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-27
      • 2011-11-04
      相关资源
      最近更新 更多