springboot处理不同域的前端请求

一. 添加一个配置类 *.*.*.config.*Configuration,该类实现WebMvcConfigurer接口,在该类中添加以下代码即可

package com.hzh.springboot_vue.config;

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

@Configuration
public class CrosConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET","HEAD","POST","PUT","DELETE","OPTIONS")
                .allowCredentials(true)
                .maxAge(3600)
                .allowedHeaders("*");
    }
}
原文地址:https://www.cnblogs.com/hzh-666/p/12250125.html