1. 后端解决
直接上代码:
package com.cai.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* Created by xxx on 2021/8/28
*/
@Configuration // 全局配置
public class CrosConfig implements WebMvcConfigurer {
@Override // 继承webMvc配置类,重写跨域CorsMappings
public void addCorsMappings(CorsRegistry registry) {
// 设置允许跨域的路径
registry.addMapping("/**") // 所有
// 设置允许跨域请求的域名
.allowedOriginPatterns("*")
// 是否允许证书
.allowCredentials(true)
// 设置允许的方法
.allowedMethods("GET", "POST", "DELETE", "PUT") // 所有
// 设置允许的header属性
.allowedHeaders("*") // 所有
// 跨域允许时间
.maxAge(3600);
}
}
(才发现这个也以前就发过…这里)
2. nginx反向代理解决
才发现之前已写过,转到我另一篇帖子:这里