简体   繁体   English

Graphql Apollo客户端看不到授权标头

[英]Graphql Apollo Client can't see Authorization header

I need to work with Graphql development server which requires from user Basic authentication. 我需要使用需要用户基本身份验证的Graphql开发服务器。

在此处输入图片说明 On frontend side to make requests to protected graphql service I wrote next code 在前端,向受保护的graphql服务发出请求,我编写了下一个代码

const authLink = setContext((_, { headers }) => {
   return {
      headers: {
         ...headers,
         Authorization: 'Basic ' + btoa('<login>:<pass>'),
      }
   }
});

const httpLink = new HttpLink({
   uri: process.env.REACT_APP_GQL_SERVER,
   fetchOptions: {
      mode: 'no-cors'
   }
});

const client = new ApolloClient({
   link: authLink.concat(httpLink),
   cache: new InMemoryCache(),
});

But I can't see "Authorization" header in the browser while request 但是请求时我无法在浏览器中看到“授权”标头 在此处输入图片说明

Could you please support me to paste Authorization header to the request or understand another way to work with Default Browser Authentication Prompt. 您能否支持我将“授权”标头粘贴到请求中,或者了解使用“默认浏览器身份验证提示”的另一种方法。

using: "apollo-boost": "^0.1.22", "apollo-link-context": "^1.0.12", 使用:“ apollo-boost”:“ ^ 0.1.22”,“ apollo-link-context”:“ ^ 1.0.12”,

============================================= =============================================

Tested variant to put header #1 经过测试的变体放置标头#1

============================================= =============================================

const httpLink = createHttpLink({
   uri: process.env.REACT_APP_GQL_SERVER,
   fetchOptions: {
      mode: 'no-cors'
   },
});

const middlewareLink = new ApolloLink((operation, forward: any) => {
  operation.setContext({
    headers: {
      "Authorization": 'Basic ' + btoa('<login>:<password>')
    }
  });
  return forward(operation);
});

const client = new ApolloClient({
   link: middlewareLink.concat(httpLink),
   cache: new InMemoryCache(),
});

============================================= =============================================

Tested variant to put header #2 经过测试的变体可以放置标头#2

============================================= =============================================

const authLink = setContext((_, { headers }) => {
  return {
    headers: {
      ...headers,
      authorization: 'Basic ' + btoa('<login>:<password>'),
    }
  }
});

const httpLink = new HttpLink({
   uri: process.env.REACT_APP_GQL_SERVER,
   fetchOptions: {
      mode: 'no-cors'
   }
});

const links: any = [];
links.push(httpLink);
links.push(authLink);

const client = new ApolloClient({
   link: ApolloLink.from(links),
   cache: new InMemoryCache(),
});

============================================= =============================================

Tested variant to put header #3 经过测试的变体放置标头3

============================================= =============================================

const middlewareLink = new ApolloLink((operation, forward: any) => {
  operation.setContext({
    headers: {
      authorization: 'Basic ' + btoa('<login>:<password>')
    }
  });
  return forward(operation);
});


const httpLink = new HttpLink({
   uri: process.env.REACT_APP_GQL_SERVER,
   fetchOptions: {
      mode: 'no-cors'
   }
});

const links: any = [];
links.push(httpLink);
links.push(middlewareLink);

const client = new ApolloClient({
   link: ApolloLink.from(links),
   cache: new InMemoryCache(),
});

Shortness: 短度:

BAP - BROWSER AUTH PROMPT BAP-浏览器授权提示

Answer: 回答:

I worked around a lot and I found a solution: 我做了很多工作,然后找到了解决方案:

  1. Add OPTIONS request as allowed on backend. 在后端允许的情况下添加OPTIONS请求。 Response for OPTIONS request should be always 200 or 2xx. 对OPTIONS请求的响应应始终为200或2xx。 We need to exclude OPTIONS request from BAP checking because by specification - OPTIONS request can't have 'Authorization' header, this header will be always removed from request and if BAP will check required 'Authorization' header in OPTIONS request ofcourse it will not be found. 我们需要从BAP检查中排除OPTIONS请求,因为根据规范-OPTIONS请求不能具有“ Authorization”标头,该标头将始终从请求中删除,如果BAP在OPTIONS请求中检查所需的“ Authorization”标头,则不会找到了。
  2. And I have created apollo-client in this way 我以这种方式创建了apollo-client

     import gql from 'graphql-tag'; import { ApolloClient, ApolloLink, HttpLink, InMemoryCache, FetchPolicy } from 'apollo-boost'; import { onError } from 'apollo-link-error'; const errorLink = onError(({ graphQLErrors, networkError }) => { if (graphQLErrors) { graphQLErrors.map(({ message, locations, path }) => console.error( `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`, ), ); } if (networkError) { logger.error(`[Network error]: ${networkError}`); } }); const links: any = []; links.push(errorLink); links.push( new HttpLink({ uri: process.env.REACT_APP_GQL_SERVER, headers: { authorization: 'Basic ' + btoa('<login>:<password>') }, }), ); const client = new ApolloClient({ link: ApolloLink.from(links), cache: new InMemoryCache(), }); 
    1. BAP was enabled while no web-site auth was implemented. 未实施网站身份验证时启用了BAP Of course, in production mode you will need to disable BAP and solution described here will cause another issues, so in future this solution should be reverted. 当然,在生产模式下,您将需要禁用BAP,并且此处描述的解决方案还会引起其他问题,因此将来应恢复此解决方案。

If you see another solution please help mу to find the best way of doing :) 如果您看到其他解决方案,请帮助您找到最佳的方法:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法转换/规范化/修改 GraphQL 查询的响应 - Apollo Client - Can't transform/normalize/modify response of GraphQL query - Apollo Client 使用 Apollo 客户端在 nextJs 中通过授权 header 的最佳方法? ReferenceError: localStorage 未定义 - The best way to pass authorization header in nextJs using Apollo client? ReferenceError: localStorage is not defined 无法在标头请求上设置授权? - can't set authorization on header request? GraphQL变异后的updateQueries无法与Apollo客户端一起使用 - updateQueries after GraphQL mutation not working with the Apollo client 将Redux客户端从REST转换为GraphQL Apollo吗? - Converting redux client from REST to GraphQL Apollo? Apollo客户端Graphql查询变量类型错误 - Apollo client Graphql query variable type error Graphql Playground 和 Apollo Client 之间的结果不一致 - Inconsistent results between Graphql playground and Apollo Client 反应中的奇怪错误 - 使用阿波罗/客户端 - graphql - Weird error in react - using apollo/client - graphql 我在 apollo-client 缓存中的组合 graphql 查询是否正确? 如果没有,我怎样才能使它成为可能? - Will my combined graphql queries in apollo-client cache correctly? And if not, how can I make it possible? 如何为Apollo Client指定GraphQL查询并获取属性出现适当字符串的项目 - How I can specify GraphQL query for Apollo Client and get items which property has an occurrence of the appropriate string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM