Spring Boot+Vue一步跨域

前段时间做个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);
    }
}

发布了26 篇原创文章 · 获赞 3 · 访问量 1420

猜你喜欢

转载自blog.csdn.net/CQWNB/article/details/104562845