我有一个使用 spring-mvc + tomcat 的项目。它有一个keycloak适配器和一个CMS系统,使用传统会话来管理用户登录,
即 spring-mvc 检查会话是否有权限,如果没有,它将重定向到 keycloak 登录页面。
我需要为他们的新移动应用程序编写一些新路由(RESTful,使用 @RestController
)。如果它们有权限/令牌有效,这些 api 将接受 access_token 并返回数据。
因为这个后端还需要支持旧的CMS系统,所以我无法将spring设置为无状态或禁用会话使用。
由于我无法控制谁在使用这些新的 RESTful API,一些 api 用户只是调用这些 api,而不传递会话 cookie,这样后端每次调用时都会为他们创建一个新会话(这些 api 将被称为非常频繁更新数据,比如每分钟30次)
那么,如果会话太多,服务器会出现内存使用问题吗?我知道会话超时默认应该是30分钟,这个超时足够吗?我已经做了很多搜索,但似乎没有人谈论这个
I am having a project using spring-mvc + tomcat. It has a keycloak adapator and a CMS systems which use the traditional session to manage the user login,
i.e. the spring-mvc checks if the session has permission, and if not it will redirect to keycloak login page.
I need to write some new routes (RESTful, using @RestController
) for their new mobile app. These api will accept the access_token and return data if they have permission/ the token is valid.
Because this backend needs to also support the old CMS system, so I can't set the spring to stateless or disable the session usage.
As I am not in controll of who is using these new RESTful API, some api users are just calling these api without passing the session cookies, so that the backend makes a new session for them every time they called (these api will be called very frequently to update the data, say 30 every mins)
So, will the server having memory usage problem if there are too many sessions? I know that the default of session timeout should be 30mins, is this timeout enough?. I have done a lot of searching but seems no one talks about this
发布评论
评论(2)
每个会话都会消耗一些内存,会话所需的总内存为
会话数量(并行)
x每个会话大小
。 - 我知道这没有帮助,所以接下来是完整的帮助部分。如果您有许多(巨大的)会话,Tomcat 可以将它们保存在磁盘上,而不是将它们保存在内存中。您只需要为会话配置其他 Manger 实现:切换到 org.apache.catalina.session.PercientManager 并需要配置idle 参数:https://tomcat.apache.org/tomcat-9.0-doc/config/manager.html
重要提示:会话中存储的所有内容都必须是
可序列化
! 。Each session will consume some memory, to the total needed memory for sessions is
number of sessions (in parallel)
xsize per session
. - I know this is not helpful, so the help full part comes next.If you have many (huge) sessions, Tomcat can persist them on disk, instead of hold them in memory. You just need to configure an other Manger Implementation for sessions: switch to
org.apache.catalina.session.PersistentManager
and you need to configure theidle
parameters: https://tomcat.apache.org/tomcat-9.0-doc/config/manager.htmlImportant: all the stuff that is stored in your session must be
Serializable
!.我终于找到了一种方法,可以在 Web cms 的路由中实际启用会话,同时禁用 RESTful 路由上的会话,我将在此处发布它,
您可以定义一个
MultiHttpSecurityConfig
I finally figured out a way to actually enables sessions in routes for web cms while disabling sessions on RESTful routes, i will post it here
you can define a
MultiHttpSecurityConfig