WebMvcConfig.java 1.4 KB
package cn.com.glsx.entmicro.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author yelq
 * @Version 1.0
 * @Description: 跨域处理
 * @Date 2022/9/23 10:54
 */
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private Environment environment;


    @Override
    public void addCorsMappings(CorsRegistry corsRegistry){
        String mappings = "/picture/compression/upload,/file/upload,/video/compression/upload";
        String origins = environment.getProperty("entmicro.allowed-origins");
        if (origins==null){
            origins = "http://localhost:8080";
        }
        for (String mapping : mappings.split(",")) {
            corsRegistry.addMapping(mapping)
                    .allowedOrigins(origins.split(","))
                    .allowedMethods(CorsConfiguration.ALL)
                    .allowedHeaders(CorsConfiguration.ALL)
                    .allowCredentials(true)//携带凭证
                    .maxAge(3600);
        }

    }

}