package cn.reghao.bnt.web.admin.security.session;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.security.web.session.ConcurrentSessionFilter;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;
import org.springframework.session.jdbc.JdbcIndexedSessionRepository;
import org.springframework.session.security.SpringSessionBackedSessionRegistry;

/**
 * spring-security 中涉及 session 处理的相关配置
 *
 * @author reghao
 * @date 2024-02-02 17:18:12
 */
@Configuration
public class SecuritySessionConfig {
    /**
     * spring-security 实现的 SessionRegistry
     *
     * @param
     * @return
     * @date 2025-10-02 16:10:746
     */
    /*@Bean
    public SessionRegistry sessionRegistry() {
        return new SessionRegistryImpl();
    }*/

    /**
     * spring-session 实现的 SessionRegistry
     *
     * @param
     * @return
     * @date 2025-10-02 16:10:869
     */
    @Bean
    public SessionRegistry sessionRegistry(JdbcIndexedSessionRepository sessionRepository) {
        return new SpringSessionBackedSessionRegistry(sessionRepository);
    }

    /**
     * 应用启动时自动添加到 org.apache.catalina.core.ApplicationFilterChain
     * 帐号同时登入数量超过限制时会调用 SecurityContextLogoutHandler 销毁 session 并发送 SessionInformationExpiredEvent 事件
     *
     * @param
     * @return
     * @date 2025-10-02 01:10:344
     */
    @Bean
    public ConcurrentSessionFilter concurrentSessionFilter(SessionRegistry sessionRegistry,
                                                           SessionInformationExpiredStrategy sessionExpiredStrategy){
        return new ConcurrentSessionFilter(sessionRegistry, sessionExpiredStrategy);
    }
}
