首页 > 其他分享 >SpringBoot 2.4.0版本后解决跨域问题

SpringBoot 2.4.0版本后解决跨域问题

时间:2022-11-29 10:38:36浏览次数:37  
标签:web SpringBoot CorsConfiguration springframework org import corsConfiguration 2.


package com.atguigu.gulimall.gateway.config;

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 xh
* @Date 2022/1/28
*/
@Configuration
public class GulimallCrosConfiguration {

@Bean
public CorsWebFilter corsWebFilter() {

CorsConfiguration corsConfiguration = new CorsConfiguration();
//1,允许任何来源
corsConfiguration.setAllowedOriginPatterns(Collections.singletonList("*"));
//2,允许任何请求头
corsConfiguration.addAllowedHeader(CorsConfiguration.ALL);
//3,允许任何方法
corsConfiguration.addAllowedMethod(CorsConfiguration.ALL);
//4,允许凭证
corsConfiguration.setAllowCredentials(true);

UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", corsConfiguration);
return new CorsWebFilter(source);
}
}

可能会报的错:

SpringBoot 2.4.0版本后解决跨域问题_spring


导错包啦!

SpringBoot 2.4.0版本后解决跨域问题_请求头_02


标签:web,SpringBoot,CorsConfiguration,springframework,org,import,corsConfiguration,2.
From: https://blog.51cto.com/u_15895329/5894196

相关文章