package cn.teammodel.config;

import cn.teammodel.config.intercepter.EnhanceRequestServletFilter;
import cn.teammodel.config.intercepter.UploadApiLogInterceptor;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * WebMvc配置 : 静态文件、拦截器等
 * @author winter
 * @create 2024-01-15 17:06
 */
@Configuration
@RequiredArgsConstructor
public class WebMvcConfig implements WebMvcConfigurer {
    private final UploadApiLogInterceptor uploadApiLogInterceptor;
    /**
    * 拦截器配置
    */
    @Override
    public void addInterceptors(@NotNull InterceptorRegistry registry) {
        registry.addInterceptor(uploadApiLogInterceptor);
    }

    // 注册过滤器
    @Bean
    public FilterRegistrationBean<EnhanceRequestServletFilter> filterRegistrationBean(){
        FilterRegistrationBean<EnhanceRequestServletFilter> filterRegistrationBean=new FilterRegistrationBean<>();
        filterRegistrationBean.setFilter(new EnhanceRequestServletFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        //order的数值越小 则优先级越高,这里直接使用的最高优先级
        filterRegistrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return filterRegistrationBean;
    }
}
