SpringBoot-跨域访问
作者:互联网
单个接口实现跨域
在controller 方法上加 @CrossOrigin 注解
全局跨域处理
自定义 WebMvcConfigurer 注册到容器里,可以执行访问的路径
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration(proxyBeanMethods = false)
public class MyCorsConfiguration {
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/api/**");
}
};
}
}
标签:SpringBoot,springframework,public,访问,org,WebMvcConfigurer,import,annotation,跨域 来源: https://www.cnblogs.com/tanglianjie/p/16083215.html