package com.malk.config;

import com.malk.filter.AuthInterceptor;
import com.malk.filter.RequestInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfiguration implements WebMvcConfigurer {

    // 指定类输出日志到指定文件夹
    private static final Logger logger = LoggerFactory.getLogger("point");

    @Autowired
    private AuthInterceptor authInterceptor;

    // 请求拦截
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        logger.info("拦截器 ▷ 初始化");
        registry.addInterceptor(new RequestInterceptor())
                .addPathPatterns("/**")
                // ppExt: 若无需对外访问, 不用添加路径. static/public 为默认
                .excludePathPatterns("/assets/**", "/templates/**");

        // 请求签名校验（capability: request-auth, add-request-auth-replay-guard）
        // 默认 mjava.auth.enabled=false 时 Interceptor 内部直接返回 true，不影响现有行为
        registry.addInterceptor(authInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/assets/**", "/templates/**", "/static/**", "/web2/**");
    }

    // 跨域支持: 端口不匹配也会报跨域, 若是单个控制器开放, 可使用 @CrossOrigin 注解
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        logger.info("拦截器 ▷ 开启CORS");
        registry.addMapping("/**")  // 添加映射路径
                .allowedOrigins("*")  // 放行哪些原始
                .allowCredentials(true)  // 是否发送Cookie信息
                .allowedMethods("GET", "POST", "PUT", "DELETE", "PATCH")  // 放行哪些原始域(请求方式)
                .allowedHeaders("*")  // 放行哪些原始域(头部信息)
                .allowCredentials(true);  // 放行证书
    }

    /**
     * 静态资源映射
     * -
     * 默认的静态资源路径为: classpath:/META-INF/resources/, classpath:/resources/,classpath:/static/, classpath:/public [默认路径不会进拦截器]
     * 读取的是target内容, 访问路径 assets: http://localhost:9001/api/assets/logo/logo-text.png [自定义拦截器添加路径排除: excludePathPatterns]
     * web网页路径: http://localhost:9011/api/项目路径/web2/index.html#/模块名称/home [前后端模块化, 不配置Nginx从Tomcat透出vue页面]
     * 当在SpringBoot项目内添加网页资源时，在windows服务器，需要C:\Windows\System32下添加tomcat-native-1.2.14-win32-bin.zip内x64下两个文件, 重启项目
     * -
     * ppExt: ClassPathResource, 需要打包/编译后才能访问到. 识别不是架包内内容 [static 可自动过滤, 自动识别子文件夹作为 path]
     * [两个示例]: mjs http://localhost:9001/api/项目路径/mjs/mjs.min.js / http://localhost:9001/api/项目路径/json/personnel.json
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/web2/**").addResourceLocations("classpath:/static/web2/");
        registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/assets/");
        registry.addResourceHandler("/templates/**").addResourceLocations("classpath:/templates/");
    }
}
