springboot 跨域

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。

posted @ 2018-03-15 15:13  yangfei969  阅读(155)  评论(0编辑  收藏  举报