如果 spring-mvc 中有太多会话会发生什么?雄猫应用程序

发布于 2025-01-10 14:56:53 字数 491 浏览 0 评论 0 原文

我有一个使用 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩人痒 2025-01-17 14:56:53

每个会话都会消耗一些内存,会话所需的总内存为会话数量(并行) 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) x size 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 the idle parameters: https://tomcat.apache.org/tomcat-9.0-doc/config/manager.html

Important: all the stuff that is stored in your session must be Serializable!.

孤君无依 2025-01-17 14:56:53

我终于找到了一种方法,可以在 Web cms 的路由中实际启用会话,同时禁用 RESTful 路由上的会话,我将在此处发布它,

您可以定义一个 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();
        }
    }
}

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

@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();
        }
    }
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文