springboot2.1.3跨域问题

页面问题:

Access to XMLHttpRequest at xxx’ from origin ‘http://xx.xx.xx’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
方式一:

解决方式两种:

全局:

@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedOrigins("http://xx.xx.com")
                        .allowCredentials(true)
                        .allowedMethods("*");
            }
        };
    }
}

方式二:
类或方法
@CrossOrigin(origins = “http://xx.xx.com”,allowCredentials = “true”, maxAge = 3600)

你可能感兴趣的:(springboot)