Solve front-end and back-end cross-domain errors: has been blocked by CORS policy: No 'Access-Control-Allow-Origin'

1. Error message

has been blocked by CORS policy: No 'Access-Control-Allow-Origin'
 header is present on the requested resource.

A page from one IP port (front-end project) needs to access resources on another IP port (springboot request interface), which will generate cross-domain access.

2. Solution: Add configuration class CorsConfig

@Configuration
public class CorsConfig implements WebMvcConfigurer {
    
    

    @Override
    public void addCorsMappings(CorsRegistry registry){
    
    
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedHeaders(CorsConfiguration.ALL)
                .allowedMethods(CorsConfiguration.ALL)
                .allowCredentials(true)
                .maxAge(3600);
    }
}

Guess you like

Origin blog.csdn.net/wsaicyj/article/details/127814374