后端解决跨域问题
在springboot中加入 (config文件中加入)
package com.yph.config;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.CorsRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;/** 解决跨域问题* */@Configurationpublic class CrosConfig implements WebMvcConfigurer {/*** 开启跨域*/@Overridepublic void addCorsMappings(CorsRegistry registry) {registry.addMapping("/**")// 设置允许跨域请求的域名.allowedOriginPatterns("*") //这里会报错 原因是SpringBoot升级2.4.0所出现的问题 将.allowedOrigins替换成.allowedOriginPatterns即可// 设置允许的方法.allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")// 是否允许证书(cookies).allowCredentials(true)// 跨域允许时间.maxAge(3600).allowedHeaders("*");}}
