Created
December 5, 2019 06:21
-
-
Save gjp0609/5de5929fb4b84e1b0733e31d779e7ac2 to your computer and use it in GitHub Desktop.
Springboot1.x 管理CORS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
public class GlobalCorsConfig { | |
@Bean | |
public FilterRegistrationBean corsFilter() { | |
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); | |
CorsConfiguration config = new CorsConfiguration(); | |
config.setAllowCredentials(true); | |
// 设置你要允许的网站域名,如果全允许则设为 * | |
config.addAllowedOrigin("*"); | |
// 如果要限制 HEADER 或 METHOD 请自行更改 | |
config.addAllowedHeader("*"); | |
config.addAllowedMethod("*"); | |
source.registerCorsConfiguration("/**", config); | |
FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source)); | |
// 这个顺序很重要哦,为避免麻烦请设置在最前 | |
bean.setOrder(0); | |
return bean; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment