springboot 解决跨域 Access to XMLHttpRequest at

访问报错,跨域问题

No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

springboot 配置跨域:

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 {

    @Override
    public void addCorsMappings(CorsRegistry registry) {

        registry.addMapping("/**")

                .allowedOrigins("*")

                .allowCredentials(true)

                .allowedMethods("GET", "POST", "DELETE", "PUT")

                .maxAge(3600);

    }
}

一定要加@Configuration注解,之后重启就可以了

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页