Skip to content
This repository has been archived by the owner on Nov 21, 2020. It is now read-only.

Latest commit

 

History

History
28 lines (22 loc) · 747 Bytes

cors.md

File metadata and controls

28 lines (22 loc) · 747 Bytes

跨域工具类

有时候我们的项目需要允许跨域,Spring Boot的跨域设置其实已经很简单了,但是还不够简单,现在我们在Spring Boot项目中只需要下面几行代码即可实现跨域

@Bean
public CorsFilter corsFilter() {
    return CorsUtils.newCorsFilter();
}

或者这样,只允许指定路径可以跨域

@Bean
public CorsFilter corsFilter() {
    return CorsUtils.newCorsFilter("/api/**");
}

还或者这样,自定义跨域

@Bean
public CorsFilter corsFilter() {
    return CorsUtils.newCorsFilter(CorsLane.create().setPath("/**").setCredential(true).setHeaders("*").setMethods("GET", "POST", "DELETE", "PUT", "OPTIONS").setOrigins("yourdomain.com"));
}