1、这个问题怎样解决,这里我们后端端口是9000端口,前端是8080端口
1.1 如何解决跨域访问的问题:在util文件夹下创建WebConfig类,通过这个类来解决跨域访问的配置
2、添加@Configuration来生命一个全局的配置类,配置好全局都生效
3、继承相关的类,使他能够进行跨域访问配置
4、alt + insert 重写方法,
5、addCorsMapping方法
6、addMapping什么路径可以进行访问
7、allowOrigin是对那个端口进行跨域访问
8、allowedMethods运行什么请求
9、允不允许凭证
10、配置完就可以获的信息了
11、相关配置类:
package com.example.ymzting_dm_web.util; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration public class WebConfig extends WebMvcConfigurerAdapter { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("http://localhost:8080") .allowedMethods("GET","POST","PUT","OPTIONS","DELETE") .allowCredentials(true) .maxAge(3600); } }