1. 添加关于CORS的端点配置,默认情况下是禁用的,通过以下配置打开
endpoints.cors.allowed-origins=http://www.xxx.com
endpoints.cors.allowed-methods=GET,POST,PUT,DELETE
2. 添加@CrossOrigin注解来实现跨域
3. 方法配置
import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration @EnableWebMvc public class WebConfig extends WebMvcConfigurerAdapter{ @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("http://www.www.com") .allowedMethods("GET","POST","PUT","DELETE"); } }
实际上,spring 4.2 以后才支持CORS。
原文地址:https://www.cnblogs.com/yangfei-beijing/p/8573757.html
时间: 2024-10-08 04:39:54