CorsConfig.java
package com.archiweb.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class CorsConfig {
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOriginPatterns("*") // autorise toutes les origines
.allowedMethods("GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS") // autorise tous les types de requĂȘtes
.allowedHeaders("*") // autorise tous les headers
.exposedHeaders("Authorization", "Content-Type") // expose les headers nécessaires
.allowCredentials(true) // permet les cookies / tokens
.maxAge(3600); // cache les preflight requests pendant 1 heure
}
};
}
}