首页 > 技术文章 > java后端解决跨域问题

minmin123 2020-08-25 15:42 原文

1、新建一个类,实现WebMvcConfigurer

 1 import org.springframework.context.annotation.Configuration;
 2 import org.springframework.web.servlet.config.annotation.CorsRegistry;
 3 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 4 
 5 @Configuration
 6 public class CrosConfig implements WebMvcConfigurer {
 7 
 8     @Override
 9     public void addCorsMappings(CorsRegistry registry) {
10         registry.addMapping("/**")
11                 .allowedOrigins("*")
12                 .allowedMethods("GET","HEAD","POST","PUT","DELETE","OPTIONS")
13                 .allowCredentials(true)
14                 .maxAge(3600)
15                 .allowedHeaders("*");
16 
17 
18     }
19 }

这样既可解决跨域问题

推荐阅读