package com.cloud.attendance.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;
|
|
/**
|
* 跨域配置
|
*
|
* @author bsk
|
*/
|
//@Configuration
|
public class CrossDomainConfig {
|
|
/**
|
* 跨域支持
|
*
|
* @return
|
*/
|
@Bean
|
public CorsFilter corsFilter(){
|
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
final CorsConfiguration config = new CorsConfiguration();
|
config.setAllowCredentials(true); // 允许cookies跨域
|
config.addAllowedOrigin("*");// #允许向该服务器提交请求的URI,*表示全部允许
|
config.addAllowedHeader("*");// #允许访问的头信息,*表示全部
|
config.setMaxAge(18000L);// 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了
|
config.addAllowedMethod("*");// 允许提交请求的方法,*表示全部允许
|
source.registerCorsConfiguration("/**", config);
|
return new CorsFilter(source);
|
}
|
|
//此种方式(前端和后端分开部署,前端域名和后端域名不一致时)不生效、有待研究
|
// @Bean
|
// public WebMvcConfigurer corsConfigurer() {
|
// return new WebMvcConfigurerAdapter() {
|
// @Override
|
// public void addCorsMappings(CorsRegistry registry) {
|
// registry.addMapping("/**") // 拦截所有权请求
|
// .allowedMethods("*") // 允许提交请求的方法,*表示全部允许
|
// .allowedOrigins("*") // #允许向该服务器提交请求的URI,*表示全部允许
|
// .allowCredentials(true) // 允许cookies跨域
|
// .allowedHeaders("*") // #允许访问的头信息,*表示全部
|
// .maxAge(18000L); // 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了
|
// }
|
// };
|
// }
|
|
}
|