package com.ruoyi.credit.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.ruoyi.credit.interceptor.ApiLoggingInterceptor;
import com.ruoyi.credit.interceptor.ApiSignatureInterceptor;
import com.ruoyi.credit.interceptor.IpWhitelistInterceptor;
import com.ruoyi.credit.interceptor.RateLimitInterceptor;

/**
 * API安全配置
 * 
 * 配置API安全相关的拦截器，包括限流、签名验证、IP白名单、日志记录等
 * 
 * @author ruoyi
 * @date 2025-12-19
 */
@Configuration
public class ApiSecurityConfig implements WebMvcConfigurer {

    @Autowired
    private RateLimitInterceptor rateLimitInterceptor;

    @Autowired
    private ApiSignatureInterceptor apiSignatureInterceptor;

    @Autowired
    private IpWhitelistInterceptor ipWhitelistInterceptor;

    @Autowired
    private ApiLoggingInterceptor apiLoggingInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 1. API调用日志拦截器 - 最先执行，记录所有API调用
        registry.addInterceptor(apiLoggingInterceptor)
                .addPathPatterns("/api/**")
                .order(1);

        // 2. IP白名单拦截器 - 第二执行，过滤不允许的IP
        registry.addInterceptor(ipWhitelistInterceptor)
                .addPathPatterns("/api/**")
                .order(2);

        // 3. 限流拦截器 - 第三执行，控制访问频率
        registry.addInterceptor(rateLimitInterceptor)
                .addPathPatterns("/api/**")
                .order(3);

        // 4. API签名验证拦截器 - 最后执行，验证请求签名
        registry.addInterceptor(apiSignatureInterceptor)
                .addPathPatterns("/api/**")
                .order(4);
    }
}