当出现跨域问题时
在spring boot项目中创建一个类
如下
package com.demo2.demo.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class CrosConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("GET","HEAD","POST","PUT")
.allowCredentials(true)
.maxAge(3600)
.allowedHeaders("*");
}
}
这样跨域问题就解决了