程序员社区

SpringBoot 2.4.0 版本之后跨域的问题 处理

 

package com.clickpaas.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;

import java.util.Collections;

/**
 * @java项目www.fhadmin.org
 * @version 1.0
 * @since 2020/11/12 7:59 下午
 */
@Configuration
public class CorsConfig {
    @Bean
    public CorsFilter corsFilter() {
        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 CorsFilter(source);
    }
}

wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

 

赞(0) 打赏
未经允许不得转载:IDEA激活码 » SpringBoot 2.4.0 版本之后跨域的问题 处理

相关推荐

  • 暂无文章

一个分享Java & Python知识的社区