springboot 跨域

Posted yangfei969

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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。

以上是关于springboot 跨域的主要内容,如果未能解决你的问题,请参考以下文章