添加 cors 策略 允许跨域

This commit is contained in:
shikong 2023-10-10 16:58:10 +08:00
parent 765b1f6fe8
commit 3ea5ac5deb

View File

@ -5,6 +5,7 @@ import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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.WebMvcConfigurer;
@ -19,4 +20,14 @@ public class WebConfig implements WebMvcConfigurer {
.excludePathPatterns("/swagger-ui/**","/v3/api-docs/**")
.addPathPatterns("/**");
}
@Override
public void addCorsMappings(CorsRegistry corsRegistry){
corsRegistry.addMapping("/**")
.allowedOrigins("*")
.allowCredentials(true)
.allowedMethods("GET","POST","HEAD","DELETE","PUT")
.allowedHeaders("*")
.maxAge(3600);
}
}