springboot2.0跨域配置:
一、代码
-
@Value
(
“${cors.allowOrigin.url:anyValue}”
)
-
private
String corsAllowOriginUrl;
-
@ConditionalOnProperty
(name=
“cors.allowOrigin.enable”
, havingValue=
“true”
, matchIfMissing=
false
)
-
@Bean
-
public
CorsFilter corsFilter() {
-
final
UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource =
new
UrlBasedCorsConfigurationSource();
-
final
CorsConfiguration corsConfiguration =
new
CorsConfiguration();
-
corsConfiguration.setAllowCredentials(
true
);
-
corsConfiguration.addAllowedOrigin(
“*”
);
-
corsConfiguration.addAllowedHeader(
“*”
);
-
corsConfiguration.addAllowedMethod(
“*”
);
-
log.debug(
“启用跨域,允许域名:{}”
,corsAllowOriginUrl);
-
urlBasedCorsConfigurationSource.registerCorsConfiguration(corsAllowOriginUrl, corsConfiguration);
-
return
new
CorsFilter(urlBasedCorsConfigurationSource);
-
}
二、application.properties
可以通过参数cors.allowOrigin.enable,控制是否允许跨域。
-
cors.allowOrigin.url=https:
//servicewechat.com
-
cors.allowOrigin.enable=
false
版权声明:本文为frank1998819原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。