package com.weiwojc.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; @Configuration public class CorsConfig { @Bean public CorsFilter corsFilter() { UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); CorsConfiguration config = new CorsConfiguration(); // 允许跨域的源,这里设置为允许所有源 config.addAllowedOriginPattern("*"); // 允许跨域的请求头 config.addAllowedHeader("*"); // 允许跨域的请求方法 config.addAllowedMethod("*"); // 允许携带认证信息(token) config.setAllowCredentials(true); // 暴露响应头 config.addExposedHeader("*"); // 设置跨域请求的有效期,单位为秒 config.setMaxAge(3600L); source.registerCorsConfiguration("/**", config); return new CorsFilter(source); } }