package com.example.demo.cors; 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.WebMvcConfigurationSupport; /** * springboot 项目间跨域问题 CORS * @author * */ @Configuration public class CustomCorsConfiguration { @Bean public WebMvcConfigurationSupport getConfigurer() { return new WebMvcConfigurationSupport() { @Override protected void addCorsMappings(CorsRegistry registry) { registry.addMapping("/api/**")//允许访问的接口 .allowedOrigins(new String[] {"http://localhost:8081"});//允许哪些站点访问该接口 } }; } }