Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.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
Reactjs fetch()在添加spring安全模块时不发送头?_Reactjs_Spring Boot_Cors_Graphql_React Apollo - Fatal编程技术网

Reactjs fetch()在添加spring安全模块时不发送头?

Reactjs fetch()在添加spring安全模块时不发送头?,reactjs,spring-boot,cors,graphql,react-apollo,Reactjs,Spring Boot,Cors,Graphql,React Apollo,我最近在我的项目中添加了Spring安全性(react apollo graphQl/springboot),看起来我无法发送标题??我想是科尔斯 我的代码使用apollo客户端进行响应: const httpLink = new HttpLink({ uri: 'http://localhost:8080/graphql' }); const authLink = new ApolloLink((operation, forward) => { operation.setContex

我最近在我的项目中添加了Spring安全性(react apollo graphQl/springboot),看起来我无法发送标题??我想是科尔斯

我的代码使用apollo客户端进行响应:

const httpLink = new HttpLink({ uri: 'http://localhost:8080/graphql' });
const authLink = new ApolloLink((operation, forward) => {
  operation.setContext({
    headers: {
       'Content-Type': 'application/json',
       'Authorization': `Bearer ${token}`
    }
  });
      return forward(operation);
    });

export const clientBackEnd = new ApolloClient({
  link: authLink.concat(httpLink), 
  cache: new InMemoryCache()
});
spring安全配置:

 @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
         httpSecurity.csrf().disable()
                .authorizeRequests().antMatchers("/authenticate").permitAll().
                anyRequest().authenticated().and().sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        httpSecurity.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
    }
Cors配置:

public class CorsConfig {
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/graphql").allowedOrigins("http://localhost:3000", "http://localhost:3001");
            }
        };
    }
}
开发工具:

看起来标题甚至没有发送!?


有人能解决这个问题吗!我做错了什么

正如在进一步评论中所讨论的,该问题是基于将请求生成的来源列入白名单

因此,问题基本上是——我们需要通过将请求将来自或发起的域列为白名单来允许源服务器端

所以这里的客户机在3000中运行,所以当它请求数据时,我们需要将该端口列为白名单并返回响应


有关白名单和避免cors问题的信息,请参阅

您是否收到了cors问题?客户端是否运行端口为3000,请尝试*并检查它是否正常工作。基本上,您需要在添加AllowedOriginates try with*的代码中列出白名单,并检查其工作情况Yeah:从源代码“”获取“”的访问是否已被CORS策略阻止:对飞行前请求的响应未通过访问控制检查:它没有HTTP ok状态。是的,它在3000上运行,我刚刚试过*仍然是同一个错误有cors扩展你可以安装并试用它,飞行前模式基本上是为了安全目的我试过访问控制允许Origin Chrome扩展,仍然是同一个错误。我甚至试着将浏览器从chrome改为Firefox,改成相同的错误。这与浏览器问题无关,基本上是服务器端的允许组织,我们需要将请求提交的域变为白色