前段时间做个Spring Boot + Vue的小工程发现自己已经忘记的怎么跨域请求、接收数据了。
在工程中创建一个包放进去,工程编译既可自动使用。
package com.study.it.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 CorsConfig extends WebMvcConfigurerAdapter {
static final String ORIGINS[] = new String[] { "GET", "POST", "PUT", "DELETE","OPTIONS" };
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*").allowCredentials(true).allowedMethods(ORIGINS).maxAge(3600);
}
}

本文分享了在SpringBoot项目中解决跨域请求问题的方法,通过创建CorsConfig配置类,设置允许所有源、凭证、方法及最大年龄,实现全局跨域访问。
1万+

被折叠的 条评论
为什么被折叠?



