如果springmvc+tomcat应用程序中有太多会话,会发生什么



我有一个使用springmvc+tomcat的项目。它有一个密钥斗篷adapator和一个CMS系统,使用传统的会话来管理用户登录,即springmvc检查会话是否具有权限,如果没有权限,它将重定向到keycapture登录页面。

我需要为他们的新移动应用程序编写一些新的路由(RESTful,使用@RestController(。如果他们有权限/令牌有效,这些api将接受access_token并返回数据。

因为这个后端还需要支持旧的CMS系统,所以我不能将spring设置为无状态或禁用会话使用。

由于我无法控制谁在使用这些新的RESTful API,一些API用户只是在不传递会话cookie的情况下调用这些API,因此后端每次调用时都会为他们创建一个新的会话(这些API将被频繁调用以更新数据,比如每分钟30次(

那么,如果会话太多,服务器是否会出现内存使用问题?我知道会话超时的默认值应该是30分钟,这个超时足够吗?。我做了很多搜索,但似乎没有人谈论这个

每个会话都会消耗一些内存,会话所需的总内存为number of sessions (in parallel)xsize per session。-我知道这没有帮助,所以接下来是帮助的全部部分

如果您有许多(巨大的(会话,Tomcat可以将它们持久化在磁盘上,而不是将它们保存在内存中。您只需要为会话配置另一个Manger Implementation:切换到org.apache.catalina.session.PersistentManager,然后您需要配置idle参数:https://tomcat.apache.org/tomcat-9.0-doc/config/manager.html

重要提示:存储在会话中的所有内容都必须是Serializable!。

我终于找到了一种方法,可以在禁用RESTful路由上的会话的同时,在路由中为web cms启用会话,我将在这里发布

可以定义MultiHttpSecurityConfig

@Configuration
public class MultiHttpSecurityConfig{

@KeycloakConfiguration
@Order(1)
public class SecurityConfig1 extends KeycloakWebSecurityConfigurerAdapter
{
/**
* Registers the KeycloakAuthenticationProvider with the authentication manager.
*/
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(keycloakAuthenticationProvider());
}
/**
* Defines the session authentication strategy.
*/
@Bean
@Override
protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
return new NullAuthenticatedSessionStrategy();
}

@Override
protected void configure(HttpSecurity http) throws Exception
{
super.configure(http);
http.antMatcher("/api/v1/external/**")  // these routes disabled session
.authorizeRequests()
.anyRequest()
.permitAll()
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
}

@KeycloakConfiguration
@Order(2)
public class SecurityConfig2 extends KeycloakWebSecurityConfigurerAdapter
{
/**
* Registers the KeycloakAuthenticationProvider with the authentication manager.
*/
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(keycloakAuthenticationProvider());
}
/**
* Defines the session authentication strategy.
*/
@Bean
@Override
protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
return new RegisterSessionAuthenticationStrategy(buildSessionRegistry());
}
@Bean
protected SessionRegistry buildSessionRegistry() {
return new SessionRegistryImpl();
}
@Override
protected void configure(HttpSecurity http) throws Exception
{
super.configure(http);
http
.antMatcher("/")
.authorizeRequests()
.anyRequest().permitAll();
}
}
}

最新更新