package org.dromara.web.config;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
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;

/**
 * Web MVC 扩展配置
 *
 * @author Pair-Programmer
 */
@Configuration
public class WebMvcPlusConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册风险控制拦截器
        registry.addInterceptor(new RiskInterceptor())
            .addPathPatterns("/system/**", "/monitor/**", "/tool/**");
    }

    @Bean
    public AppInfoCompletionFilter appInfoCompletionFilter() {
        return new AppInfoCompletionFilter();
    }

    @Bean
    public FilterRegistrationBean<AppInfoCompletionFilter> appInfoCompletionFilterRegistration(AppInfoCompletionFilter filter) {
        FilterRegistrationBean<AppInfoCompletionFilter> registration = new FilterRegistrationBean<>(filter);
        registration.addUrlPatterns("/app/*");
        registration.setName("appInfoCompletionFilter");
        // 顺序需在 Sa-Token 认证过滤器之后，但在业务逻辑之前
        // Sa-Token 拦截器在 Interceptor 层，Filter 在 Servlet 层。
        // 但 LoginHelper 需要 Token，Token 在 Header 里，随时可取。
        // 关键是 Filter 需要在请求处理前执行。
        registration.setOrder(100);
        return registration;
    }
}
