package egovframework.config;

import egovframework.configProperties.InterceptorProperties;
import egovframework.interceptor.AuthInterceptor;
import go.kr.project.login.service.LoginService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 인터셉터 설정 클래스
 */
@Configuration
@RequiredArgsConstructor
public class EgovConfigInterceptor implements WebMvcConfigurer {

    private final LoginService loginService;
    private final InterceptorProperties interceptorProperties;


    /**
     * 인터셉터 등록
     * @param registry 인터셉터 레지스트리
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authInterceptor())
                .addPathPatterns("/**") // 모든 경로에 적용
                .excludePathPatterns(interceptorProperties.getInterceptorExclude()); // 접근 제어 예외 URL 패턴 제외
    }

    /**
     * 인증 인터셉터 빈 등록
     * @return AuthInterceptor 객체
     */
    @Bean
    public AuthInterceptor authInterceptor() {
        return new AuthInterceptor(loginService);
    }
}