Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Axios未发送授权标头-ReactJS_Reactjs_Spring Boot_Axios - Fatal编程技术网

Axios未发送授权标头-ReactJS

Axios未发送授权标头-ReactJS,reactjs,spring-boot,axios,Reactjs,Spring Boot,Axios,我在axios GET请求中设置授权标头时遇到问题。 我做了很多研究,但没有找到解决办法。我还检查了CORS设置,它应该是正常的,请求是从postman或AdvanceREST客户端发出的,所以我不认为这是服务器端的问题 我的函数与axios请求 export function getUserInfo (userId) { return function (dispatch) { axios.get(`${ROOT_URL}/user/${userId}`, helperMethods

我在axios GET请求中设置授权标头时遇到问题。 我做了很多研究,但没有找到解决办法。我还检查了CORS设置,它应该是正常的,请求是从postman或AdvanceREST客户端发出的,所以我不认为这是服务器端的问题

我的函数与axios请求

export function getUserInfo (userId) {
  return function (dispatch) {
   axios.get(`${ROOT_URL}/user/${userId}`, helperMethods.authorizedHeader())
    .then(response => {
      dispatch({type: USER_INFO, payload: response.data.message});
    })
    .catch(error => {
      console.log('something went wrong: ', error);
    });
  };
}
Helper方法(返回有效对象,我调试了它)

和CORS设置:

@Bean
public CorsFilter corsFilter() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    CorsConfiguration config = new CorsConfiguration();
    config.setAllowCredentials(true);
    config.addAllowedOrigin("*");
    config.addAllowedHeader("*");
    config.addAllowedMethod("*");
    source.registerCorsConfiguration("/**", config);
    return new CorsFilter(source);
}
因此,如果你有任何建议,请与我分享


谢谢

最后,我找到了问题。问题在于服务器端的CORS配置。当请求被触发时,它首先进入SpringCORS过滤器,它拒绝请求,并且它永远不会触发我的CORS过滤器。 所以我必须设置触发顺序,类似这样:

FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
    bean.setOrder(-110);
下面是整个更新的CORS配置:

@Bean
public FilterRegistrationBean platformCorsFilter() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

    CorsConfiguration configAutenticacao = new CorsConfiguration();
    configAutenticacao.setAllowCredentials(true);
    configAutenticacao.addAllowedOrigin("*");
    configAutenticacao.addAllowedHeader("Authorization");
    configAutenticacao.addAllowedHeader("Content-Type");
    configAutenticacao.addAllowedHeader("Accept");
    configAutenticacao.addAllowedMethod("POST");
    configAutenticacao.addAllowedMethod("GET");
    configAutenticacao.addAllowedMethod("DELETE");
    configAutenticacao.addAllowedMethod("PUT");
    configAutenticacao.addAllowedMethod("OPTIONS");
    configAutenticacao.setMaxAge(3600L);
    source.registerCorsConfiguration("/**", configAutenticacao);

    FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
    bean.setOrder(-110);
    return bean;
}
@Bean
public FilterRegistrationBean platformCorsFilter() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

    CorsConfiguration configAutenticacao = new CorsConfiguration();
    configAutenticacao.setAllowCredentials(true);
    configAutenticacao.addAllowedOrigin("*");
    configAutenticacao.addAllowedHeader("Authorization");
    configAutenticacao.addAllowedHeader("Content-Type");
    configAutenticacao.addAllowedHeader("Accept");
    configAutenticacao.addAllowedMethod("POST");
    configAutenticacao.addAllowedMethod("GET");
    configAutenticacao.addAllowedMethod("DELETE");
    configAutenticacao.addAllowedMethod("PUT");
    configAutenticacao.addAllowedMethod("OPTIONS");
    configAutenticacao.setMaxAge(3600L);
    source.registerCorsConfiguration("/**", configAutenticacao);

    FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
    bean.setOrder(-110);
    return bean;
}