Springboot跨域处理

package com.ly.gateway.conf; import org.springframework.boot.SpringBootConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter; /** * @Author AnHui_XiaoYang * @Email [email protected] * @Date 2021/5/10 17:07 * @Description */ @SpringBootConfiguration public class WebGlobalConfig { @Bean public CorsFilter corsFilter() { //创建CorsConfiguration对象后添加配置 CorsConfiguration config = new CorsConfiguration(); //设置放行哪些原始域 config.addAllowedOriginPattern("*"); //放行哪些原始请求头部信息 config.addAllowedHeader("*"); //暴露哪些头部信息 config.addExposedHeader("*"); //放行哪些请求方式 config.addAllowedMethod("GET"); //get config.addAllowedMethod("PUT"); //put config.addAllowedMethod("POST"); //post config.addAllowedMethod("DELETE"); //delete //corsConfig.addAllowedMethod("*"); //放行全部请求 //是否发送Cookie config.setAllowCredentials(true); //2. 添加映射路径 UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource(); corsConfigurationSource.registerCorsConfiguration("/**", config); //返回CorsFilter return new CorsFilter(corsConfigurationSource); } }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章