springboot解决前后端分离时请求跨域

新建配置类即可

package com.hkj.springboot.apiForVue.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author liujy
 * @description
 * @since 2021-01-20 15:08
 */
@Configuration
public class CorsConfiguration implements WebMvcConfigurer {
    /**
     * @description 跨域支持
     * @author liujy
     * @date 2021/1/20 15:08
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("GET","POST","DELETE","PUT")
                .maxAge(3600*20);
    }
}

推荐阅读更多精彩内容