【发布时间】:2013-08-10 18:28:03
【问题描述】:
我有一个使用 Spring 3.1 的 java webapp。我的 Spring 安全上下文定义了多个身份验证过滤器,每个过滤器对应一个不同的身份验证路径(例如,用户名/密码与单点登录)。每个身份验证过滤器都定义了自己的AuthenticationSuccessHandler。现在,我想注入 2 个额外的操作以在成功的身份验证时采取,它们应该适用于 所有 身份验证类型:
- 设置一个跟踪事件代码供 Google Analytics 在前端使用
- 在我们的数据库中更新用户的首选语言环境
在用户成功通过身份验证后,这些可能是您想要挂钩的任何操作。重要的一点是,与常规的 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