欢迎您的访问
专注架构,Java,数据结构算法,Python技术分享

SpringBoot 2.6.7 处理跨域的问题

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;
 
/**
 * @from fhadmin.cn
 * @version 1.0
 * @since 2022/5/5 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);
    }
}
赞(0) 打赏
版权归原创作者所有,任何形式转载请联系作者;码农code之路 博客站点 » SpringBoot 2.6.7 处理跨域的问题

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏