Java学习-064-Springboot 解决跨域访问简单配置示例
作者:互联网
解决跨域访问,添加一个配置类,如下所示:
package com.fanfengping.config.cors; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter; @Configuration public class CorsConfig { private CorsConfiguration configuration() { CorsConfiguration corsConfig = new CorsConfiguration(); corsConfig.setAllowCredentials(true); corsConfig.addAllowedOrigin("*"); // 1允许任何域名使用 corsConfig.addAllowedHeader("*"); // 2允许任何头 corsConfig.addAllowedMethod("*"); // 3允许任何方法(post、get等) return corsConfig; } @Bean public CorsFilter corsFilter() { UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", configuration()); // 4 return new CorsFilter(source); } }
重新打包部署即可。
标签:Java,064,CorsConfiguration,示例,cors,springframework,corsConfig,org,import 来源: https://www.cnblogs.com/fengpingfan/p/11114121.html