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注解,之后重启就可以了

猜你喜欢

转载自blog.csdn.net/qq_28288835/article/details/109313416