Spring-Boot 2.7 -> 3 升级后,SessionCreatedEvent 不再触发

问题描述 投票:0回答:1

在Spring-Boot 2.7.x中,我收到了SessionCreatedEvent和SessionDestroyedEvent事件。升级到 Spring Boot 3.0.x 后,这些事件不再传递到我的应用程序。不过,我仍然收到 SessionFixationProtectionEvent 事件。我正在使用 Redis 进行会话管理。我尝试使用 Spring Boot 3.1.x 但没有帮助。

我的

@EnableRedisHttpSession(flushMode = FlushMode.IMMEDIATE, saveMode = SaveMode.ALWAYS, maxInactiveIntervalInSeconds = 600)
班上有
@Configuration

我已将 SecurityFilterChain 配置为:

return http
    .securityContext((securityContext) -> securityContext
        .requireExplicitSave(false)
    )
    .authorizeHttpRequests()
        .requestMatchers(EndpointRequest.toAnyEndpoint()).anonymous()
        .requestMatchers("/error", "/favicon.ico").permitAll()
        .requestMatchers(HttpMethod.GET, "/login", "/assets/**").permitAll()
        .anyRequest().authenticated()
    .and()
    .headers()
        .frameOptions().sameOrigin()
        .xssProtection().and()
    .and()
    .logout()
        .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
        .deleteCookies("JSESSIONID")
        .permitAll()
    .and()
    .sessionManagement()
        .invalidSessionStrategy(new RestfulApiInvalidSessionStrategy(new AntPathRequestMatcher("/api/**")))
        .invalidSessionUrl("/login")
        .sessionCreationPolicy(IF_REQUIRED)
        .sessionFixation().changeSessionId()
        .maximumSessions(2).maxSessionsPreventsLogin(false)
            .expiredSessionStrategy(new RestfulApiInvalidSessionStrategy(new AntPathRequestMatcher("/api/**")))
            .expiredUrl("/login?expired")
        .and()
    .and()
    .formLogin()
    .loginPage("/login").permitAll()
    .successHandler(this.authenticationSuccessHandler)
    .failureHandler(this.authenticationFailureHandler)
    .and()
    .httpBasic()
    .and()
    .csrf().disable()
    .build()

我的事件监听器是这样的:

    @EventListener
    public void onSessionCreated(SessionCreatedEvent event) {
        log.info("session created");
    }

    @EventListener
    public void onSessionDestroyed(SessionDestroyedEvent event) {
        log.info("session destroyed");
    }

    @Order(Ordered.HIGHEST_PRECEDENCE)
    @EventListener(SessionFixationProtectionEvent.class)
    public void onSessionFixationProtectionEvent(SessionFixationProtectionEvent event) {
        log.info("session migrated");
    }

我已经注册了 HttpSessionEventPublisher:

    @Bean
    public ServletListenerRegistrationBean<HttpSessionEventPublisher> httpSessionEventPublisher() {
        return new ServletListenerRegistrationBean<>(new HttpSessionEventPublisher());
    }

我不确定我还需要做什么。它在 2.7 中有效,但在 3.0 中无效。我尝试过注册

@WebListener
并监听 HttpSessionEvents,但这些似乎也没有被解雇。

spring spring-boot session events event-handling
1个回答
0
投票

为此支持 spring boot 3.2.0

@EnableRedisHttpSession
更改为 --->
@EnableRedisIndexedHttpSession
。 此更改将在 RedisSessionRepository 上配置 RedisIndexedSessionRepository

通过使用此

RedisIndexedSessionRepository
,您现在可以开始收听
SessionCreatedEvent
SessionDeletedEvent
SessionDestroyedEvent
SessionExpiredEvent
事件。

欲了解详细说明,请访问this。 要收听会话活动,请访问this

© www.soinside.com 2019 - 2024. All rights reserved.