其他分享
首页 > 其他分享> > Spring boot 和Vue开发中CORS跨域问题

Spring boot 和Vue开发中CORS跨域问题

作者:互联网

1.使用springboot做service vue做前端框架

2.遇到了CORS跨域问题

3.跨域

4.解决方法

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;

/**
 * @author Colin
 * @description
 * @date 2020-12-16
 */
@Configuration
public class CustomCORSConfiguration {
    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.setAllowCredentials(true);
        return corsConfiguration;
    }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig());
        return new CorsFilter(source);
    }
}

 

标签:Vue,跨域,corsConfiguration,Spring,springframework,CorsConfiguration,org,import
来源: https://blog.csdn.net/weixin_42350288/article/details/111301011