package com.bb.config;

import jakarta.annotation.PostConstruct;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
import org.springframework.security.web.context.SecurityContextRepository;

@Configuration // @Component보다 설정을 위한 @Configuration 권장
public class SecurityContextConfig {

    @PostConstruct
    public void init() {
        // 부모 스레드의 SecurityContext를 자식 스레드에서 상속받도록 설정
        SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_INHERITABLETHREADLOCAL);
    }

    /**
     * [핵심] 세션 저장소를 빈으로 등록하여 순환 참조 방지 및 공유
     */
    @Bean
    public SecurityContextRepository securityContextRepository() {
        return new HttpSessionSecurityContextRepository();
    }
}