package com.njyunzhi.pet_identity.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

// TODO
// 这个配置实际上不生效，不知道为什么

@Configuration
public class CorsConfig implements WebMvcConfigurer {

    @Value("${yz.cros}")
    Boolean useCROS;

    @Value("${yz.mode}")
    String mode;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        if (!useCROS) return;

//        //添加映射路径
        CorsRegistration registration = registry.addMapping("/**")
                //是否发送Cookie
                .allowCredentials(true);

        //设置放行哪些原始域
        if ("prod".equals(mode)) {
            registration.allowedOrigins("https://pet-certificate-online.njyunzhi.com");
        } else {
            registration.allowedOrigins("http://localhost:8000", "http://localhost:3000", "https://pet-certificate-online.njyunzhi.com");
        }

        //放行哪些请求方式
        registration.allowedMethods("GET", "POST", "PUT", "DELETE")
                //.allowedMethods("*") //或者放行全部
                //放行哪些原始请求头部信息
                .allowedHeaders("*")
                //暴露哪些原始请求头部信息
                .exposedHeaders("Authorization", "Content-Disposition");
    }
}


//public class CorsConfig {}