使用 Redis 进行 Spring 安全并发控制



我们有一个关于Spring Framework的项目。它包含具有以下配置的 Spring 安全性:

@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers(LOGIN_URL + "/**").permitAll()
.antMatchers("/resources/**").permitAll()
.antMatchers("/**").access(<...>)
.and()
.formLogin()
.loginPage(LOGIN_URL)
.defaultSuccessUrl(LOGIN_URL + "/success", true)
.failureUrl(LOGIN_URL + "/error")
.usernameParameter("username").passwordParameter("password")
.and()
.logout().logoutSuccessUrl(LOGOUT_URL)
.and()
.csrf()
.and()
.securityContext().securityContextRepository(reloadUserAuthoritiesService)
.and()
.sessionManagement()
.maximumSessions(1)
.sessionRegistry(sessionRegistry)
.expiredUrl(LOGIN_URL)
;
}

它适用于一台Apache Tomcat服务器。如果我尝试从其他浏览器登录,我之前的 http 会话将过期。

现在我们需要添加 Redis 服务器 (v 4.0.9( 作为 http 会话的存储,因为我们需要在多个实例之间共享会话。 但是我可以由同一用户和不同的会话登录两个Apache Tomcats。这很糟糕。 我尝试了几种方法来配置它:

  1. https://www.baeldung.com/spring-session(注释配置(
  2. 只有没有项目更改的tomcat配置(redis-session-manager-with-dependencies-2.2.2.jar(

所有这些方法都行不通。 我也发现了这个问题:https://github.com/spring-projects/spring-session/issues/65 它是两年前实施的。 有人可以帮助我吗?

谢谢大家。我忘了创建 SpringSessionBackedSessionRegistry bean。 问题现在解决了。

最新更新