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("*");
    }
}

 

posted @ 2020-02-01 20:57  hzh666  阅读(350)  评论(0编辑  收藏  举报