SpringBoot 解决VUE跨域问题

哈哈 阅读:944 2021-03-31 13:26:53 评论:0

问题产生情况描述:今天使用VUE 调用SpringBoot 后台服务接口,提示跨域,错误截图如下:

我参考网上解决Vue + SpringBoot 跨域代码,源码如下:

package com.zzg.configuration; 
 
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; 
 
/** 
 * SpringBoot CROS跨域配置对象 
 * @author Administrator 
 * 
 */ 
@Configuration 
public class GlobalCorsConfig { 
	 @Bean 
	    public CorsFilter corsFilter() { 
	        //1.添加CORS配置信息 
	        CorsConfiguration config = new CorsConfiguration(); 
	          //放行哪些原始域 
	          config.addAllowedOrigin("*"); 
	          //是否发送Cookie信息 
	          config.setAllowCredentials(true); 
	          //放行哪些原始域(请求方式) 
	          config.addAllowedMethod("*"); 
	          //放行哪些原始域(头部信息) 
	          config.addAllowedHeader("*"); 
	          // 暴露头部信息 
	          config.addExposedHeader("*"); 
	         
	        //2.添加映射路径 
	        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource(); 
	        configSource.registerCorsConfiguration("/**", config); 
 
	        //3.返回新的CorsFilter. 
	        return new CorsFilter(configSource); 
	    } 
} 

SpringBoot 重新启动,但VUE 访问SpringBoot服务接口时,提示如下错误信息:

Caused by: java.lang.IllegalArgumentException: '*' is not a valid exposed header value 
	at org.springframework.web.cors.CorsConfiguration.addExposedHeader(CorsConfiguration.java:282) 
	at com.gz.tzreport.config.GlobalCorsConfig.corsFilter(GlobalCorsConfig.java:25) 
	at com.zzg.configuration.GlobalCorsConfig$$EnhancerBySpringCGLIB$$1f6ff0f1.CGLIB$corsFilter$0(<generated>) 
	at com.zzg.configuration.GlobalCorsConfig$$EnhancerBySpringCGLIB$$1f6ff0f1$$FastClassBySpringCGLIB$$d3e777bb 

造成错误的原因:因为跨域访问默认不能获取全部头部信息,需要在addExposedHeader把各个具体的一些头部信息都添加进去而不能用*号来代替。

修改后的代码:

package com.zzg.configuration; 
 
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; 
 
/** 
 * SpringBoot CROS跨域配置对象 
 * @author Administrator 
 * 
 */ 
@Configuration 
public class GlobalCorsConfig { 
	 @Bean 
	    public CorsFilter corsFilter() { 
	        //1.添加CORS配置信息 
	        CorsConfiguration config = new CorsConfiguration(); 
	          //放行哪些原始域 
	          config.addAllowedOrigin("*"); 
	          //是否发送Cookie信息 
	          config.setAllowCredentials(true); 
	          //放行哪些原始域(请求方式) 
	          config.addAllowedMethod("*"); 
	          //放行哪些原始域(头部信息) 
	          config.addAllowedHeader("*"); 
	          //暴露哪些头部信息(因为跨域访问默认不能获取全部头部信息) 
	          // config.addExposedHeader("*"); 
	           
	          config.addExposedHeader("Content-Type"); 
	          config.addExposedHeader( "X-Requested-With"); 
	          config.addExposedHeader("accept"); 
	          config.addExposedHeader("Origin"); 
	          config.addExposedHeader( "Access-Control-Request-Method"); 
	          config.addExposedHeader("Access-Control-Request-Headers"); 
 
 
	        //2.添加映射路径 
	        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource(); 
	        configSource.registerCorsConfiguration("/**", config); 
 
	        //3.返回新的CorsFilter. 
	        return new CorsFilter(configSource); 
	    } 
} 

 

标签:VUE
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号