package com.javasm.common.config;标签:跨域,CorsConfiguration,配置,springframework,org,import,config,UrlBasedCorsConfigurat From: https://www.cnblogs.com/zyb-luckey/p/18644683
import org.apache.catalina.filters.CorsFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
/**
* @author Administrator
* @date 2024-12-31 17:47
*/
@Configuration
public class CorseConfig {
@Bean
public CorsFilter corsFilter(){
// 预先配置
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//所有的请求,都要过,都添加跨域
source.registerCorsConfiguration("/**",buildConfig());
return new CorsFilter();
}
private CorsConfiguration buildConfig(){
//配置 允许所有的作用域
CorsConfiguration config=new CorsConfiguration();
// 头信息
config.addAllowedHeader("*");
//方法
config.addAllowedMethod("*");
// cookie,session会失效
config.setAllowCredentials(true);
// 有效期
config.setMaxAge(3600L);
return config;
}
}