springboot跨域请求解决方案

springboot跨域请求解决方案
亲测有效

import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

这个 包 已经废弃 替换了

import org.springframework.web.servlet.config.annotation.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","HEAD","POST","PUT","DELETE","OPTIONS")
                .allowCredentials(true)
                .maxAge(3600)
                .allowedHeaders("*");
    }
}

备选方案
在对应Controller上添加注解。 不确定是不是姿势不对,无效
@CrossOrigin

但每个Controller都得加,太麻烦了,怎么办呢,加在Controller公共父类(PublicUtilController)中,所有Controller继承即可。


版权声明:本文为weixin_42927262原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。