0

这是使用 Spring Security 4.0 RELEASE 和 Spring Security CAS。

我正在使用 Java Config 设置会话并发管理:

http
  .sessionManagement()
  .maximumSessions(1)
  .maxSessionsPreventsLogin(false)
  .expiredUrl("/tooManySessions")
  .and()
  .and();

HttpSessionEventublisher在 a 中启用,WebApplicationInitializer我可以确认它正在工作,因为我正在将它用于其他正在工作的东西:

@Override
protected void registerDispatcherServlet(ServletContext servletContext) {
    super.registerDispatcherServlet(servletContext);


    // to handle session creation and destruction events
    servletContext.addListener(new HttpSessionEventPublisher());
}

但是在运行时,看起来代码从未被调用过。

请注意,我使用的是 Spring Security CA。这会影响会话并发管理吗?

4

1 回答 1

1

事实证明,在使用 Java Config(不了解 XML 配置)时,要让 Session Management 与 CAS 一起使用,您需要确保明确设置SessionAuthenticationStrategy(s) on CASAuthorizationFilter

我通过使用 ObjectPostProcessor 解决了这个问题CsfrFilter(在会话管理设置中这样做不会得到 Csrf 特定的SessionAuthenticationStrategy):

final CasAuthenticationFilter casAuthenticationFilter = casAuthenticationFilter();

http
        .csrf()
            .withObjectPostProcessor(new ObjectPostProcessor<CsrfFilter>() {
                @Override
                public <O extends CsrfFilter> O postProcess(O csrfFilter) {

                    try {
                        final SessionAuthenticationStrategy sessionAuthenticationStrategy = httpFinal.getSharedObject(SessionAuthenticationStrategy.class);
                        if (sessionAuthenticationStrategy == null || !(sessionAuthenticationStrategy instanceof CompositeSessionAuthenticationStrategy)) {
                            throw new IllegalStateException("Cannot get CompositeSessionAuthenticationStrategy");
                        }
                        casAuthenticationFilter.setSessionAuthenticationStrategy(sessionAuthenticationStrategy);
                    } catch (Exception e) {
                        throw new IllegalStateException("Cannot get ahold of CasAuthenticationFilter in CsrfFilter post-processor");
                    }

                    return csrfFilter;

                }
            });
}
于 2015-04-14T15:30:19.313 回答