天天看點

Java背景解決跨域問題之跨域配置類

話不多說,直接上代碼

直接在網關中配置就行

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @Author Aricon
 * @Date 2021/5/31 17:05
 * @Version 1.0
 */
@Configuration
public class CroConfig {
    //固定
    @Bean
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();

        //允許跨域的請求頭
        corsConfiguration.addAllowedHeader("*");
        //允許跨域的請求方式
        corsConfiguration.addAllowedMethod("*");
        //允許跨域的請求來源
        corsConfiguration.addAllowedOrigin("*");
        //允許攜帶cookie跨域
        corsConfiguration.setAllowCredentials(true);

        ///**代表 允許任意的請求路徑進行跨域
        source.registerCorsConfiguration("/**",corsConfiguration);

        return new CorsWebFilter(source);
    }
}

           

繼續閱讀