SpringBoot如何解决跨域Cores问题
作者:岚殿
这篇文章主要介绍了SpringBoot如何解决跨域Cores问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
SpringBoot解决跨域Cores
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 CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOriginPatterns("*")
.allowedHeaders("*")
.allowedMethods("*")
.allowCredentials(true)
.maxAge(3600);
}
}SpringBoot允许跨域
解决办法
@SpringBootApplication
@MapperScan("com.humorchen.pastry_examination.mapper")
public class PastryExaminationApplication implements WebMvcConfigurer {
public static void main(String[] args) {
SpringApplication.run(PastryExaminationApplication.class, args);
}
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowCredentials(true)
.allowedHeaders("*")
.allowedOriginPatterns("*")
.allowedMethods("*");
}
}新版本springboot跨域解决办法,把这个配置bean注入就可以了
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedHeaders("*")
.allowedMethods("*")
.allowedOrigins("*")
.allowCredentials(true);
}
};
}总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
