前端response中的自定义header信息默认同一域中可见
在crossdomain跨域情况下需要在服务器端增加Access-Control-Expose-Headers的支持
例如在springboot中需要使用addExposedHeader添加指定返回头:
@Configuration
public class CORSConfiguration implements WebMvcConfigurer {@Beanpublic CorsFilter corsFilter() {final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();final CorsConfiguration corsConfiguration = new CorsConfiguration();corsConfiguration.setAllowCredentials(true); /*是否允许请求带有验证信息*/corsConfiguration.addAllowedOrigin("*");/*允许访问的客户端域名*/corsConfiguration.addAllowedHeader("*");/*允许服务端访问的客户端请求头*/corsConfiguration.addAllowedMethod("*"); /*允许访问的方法名,GET POST等*/corsConfiguration.addExposedHeader("token");/*暴露哪些头部信息 不能用*因为跨域访问默认不能获取全部头部信息*/corsConfiguration.addExposedHeader("Authorization");urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);return new CorsFilter(urlBasedCorsConfigurationSource);}
}
成功后效果如下所示: