写一个配置类继承WebMvcConfigurerAdapter会提示这个类已经过期了,所以直接实现WebMvcConfigurerAdapter的父类WebMvcConfigurer 即可

代码如下:

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 CorsConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowedOrigins("*").allowedMethods("GET", "POST", "DELETE", "PUT").maxAge(3600);
    }
    
    /**
     * public CorsRegistration allowedOrigins(String... origins);
     * public CorsRegistration allowedMethods(String... methods);
     */

}