天天看點

spring boot 2.0 跨域問題解決,同時可以相容下載下傳靜态資源

spring boot 2.0 跨域問題解決,同時可以相容下載下傳靜态資源

@Configuration
public class CorsConfig extends WebMvcConfigurer {
 
     @Override  
        public void addCorsMappings(CorsRegistry registry) {  
            registry.addMapping("/**")  
                    .allowedOrigins("*")  
                    .allowCredentials(true)  
                    .allowedMethods("GET", "POST", "DELETE", "PUT","PATCH")  
                    .maxAge(3600);  
        }  
         @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/META-INF/resources/")
                .addResourceLocations("classpath:/resources/")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("classpath:/public/");
    }

}