发送JWT RefreshToken作为Set-Cookie

关于将JWT刷新令牌设置为cookie时是对还是错的简短问题。

我的API应该将其响应标头中的refresh token发送到ApolloServer,以便Set-Cookie标头将自动通过我的Apollo服务器向下传递到客户端(apollo客户端)。还是我应该仅从我的API中获取一个主体数据集,包括令牌+ refreshToke,我将通过apollo-server中间件提取该数据集,作为cookie附加到客户端响应本身。

有什么想法或建议吗?

xfkeshe3 回答:发送JWT RefreshToken作为Set-Cookie

必须由服务器设置Cookie。这样将使用快速响应对象来完成。确保可以从您的gql上下文访问它。​​

res.cookie('X-Refresh-Token',refreshToken,{
  maxAge: 900000,// 15 minutes in milliseconds
  httpOnly: true,// ensure the cookie will not be exposed
  secure: true,// set this in production to ensure HTTPS
  sameOrigin: 'none' // set this in production if the server is separate domain
})

然后,您的客户端必须启用凭据。对于阿波罗升压,这将是:

new ApolloClient({
  uri: 'http://localhost:8080',credentials: 'include',// 'same-origin' if your server is same domain
})

要确保cookie在GraphQL Playground中传递:

new ApolloServer({
  context: ({ req,res }) => ({ req,res }),playground: {
    settings: {
      'request.credentials': 'include',},})

如果您尚未使用它,cookie parser middleware可能会派上用场。

本文链接:https://www.f2er.com/3090173.html

大家都在问