动静分离之后,springboot处理跨域的解决方案。参考地址https://gitee.com/alohasir/manli-cloud-boot.git
package com.manlitech.cloudboot.basebootconfig.config;
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;
import java.util.Collections;
/**
- 实现基本的跨域请求
- @author
*
*/
@Configuration
public class CorsConfig {
@Bean
public CorsFilter corsFilter() {
final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
final CorsConfiguration corsConfiguration = new CorsConfiguration();
/*是否允许请求带有验证信息*/
corsConfiguration.setAllowCredentials(true);
/*springboot 2.4 之后需要注释 允许访问的客户端域名*/
//corsConfiguration.addAllowedOrigin("*");
/*springboot 2.4 之后需要增加 允许任何来源*/
corsConfiguration.setAllowedOriginPatterns(Collections.singletonList("*"));
/*允许服务端访问的客户端请求头*/
corsConfiguration.addAllowedHeader("*");
/*允许访问的方法名,GET POST等*/
corsConfiguration.addAllowedMethod("*");
urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
return new CorsFilter(urlBasedCorsConfigurationSource);
}
}