Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.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
Javascript 使用访问/刷新令牌和axios(cookies?本地存储?)进行安全身份验证_Javascript_Node.js_Reactjs_Axios - Fatal编程技术网

Javascript 使用访问/刷新令牌和axios(cookies?本地存储?)进行安全身份验证

Javascript 使用访问/刷新令牌和axios(cookies?本地存储?)进行安全身份验证,javascript,node.js,reactjs,axios,Javascript,Node.js,Reactjs,Axios,我正在构建一个身份验证系统,在react中使用一个nodejs后端的访问和刷新令牌 目前,我只使用24小时有效的访问令牌。我在服务器上的httpOnly cookie中设置它们,仅通过https发送(安全:true)。使用axios,我将withCredentials选项添加到请求中,并将cookie(带有令牌)发送到服务器 我对axios做了一些调整,以使用access/refresh令牌。到目前为止,我的代码是: import axios from 'axios'; const apiUr

我正在构建一个身份验证系统,在react中使用一个nodejs后端的访问和刷新令牌

目前,我只使用24小时有效的访问令牌。我在服务器上的httpOnly cookie中设置它们,仅通过https发送(安全:true)。使用axios,我将withCredentials选项添加到请求中,并将cookie(带有令牌)发送到服务器

我对axios做了一些调整,以使用access/refresh令牌。到目前为止,我的代码是:

import axios from 'axios';

const apiUrl = 'http://localhost:5000/api',

// add the accessToken to the request config when making a request
// with axios
axios.interceptors.request.use((config) => {
  const accessToken = localStorage.getItem('accessToken');
  if (accessToken) {
    config.headers['Authorization'] = `Bearer ${accessToken}`;
  }
  return config;
}, (error) => {
  return Promise.reject(error);
});

// check if a status 401 is send back
// this means the access token is invalid
// do a request to the server to refresh the access token with the refresh token
// and repeat the initial request when the new access token is received
axios.interceptors.response((response) => {
  // status 200 OK
  // just return the response
  return response;
}, (error) => {
  // store the original request (passed with the error)
  const origReq = error.config;
  const refreshToken = localStorage.getItem('refreshToken');
  // origReq.retried is just to prevent an infinite loop of retries
  if (refreshToken && !origReq.retried) {
    origReq.tried = true;
    return axios({
      method: 'get',
      url: `${apiUrl}/auth/refresh-access-token`,
      headers: {
        'Authorization': `Bearer ${refreshToken}`
      }
    })
    .then((response) => {
      // store the new access token
      localStorage.setItem('accessToken', response.data.accessToken);
      // retry the original request
      return axios(origReq);
    });
  } else {
    return Promise.reject(error);
  }
);
基本上,我已经创建了2个拦截器:1个用于请求(将访问令牌添加到请求中),1个用于响应(检查响应状态,如果status=401,则访问令牌无效,因此首先使用刷新令牌刷新访问令牌)

有关此代码的一些注释:

  • 在生产中,对API的所有请求都将通过HTTPS进行
  • 将访问/刷新令牌放入授权标头是否正常?在一些代码示例中,我看到他们将刷新标记放在帖子正文中。最有效/安全的方法是什么?在我的代码中,我检查了授权头,这是可行的,但我想知道什么是最好的
  • 在这个代码示例中,我将访问和刷新令牌存储在LocalStorage中。我已经找到了,但我无法阅读,它应该包含有关如何安全存储令牌的信息。说JWT令牌(就像我用来访问/刷新令牌)应该像我现在这样存储在httpOnly cookie中?但是,如何在axios请求中注入httpOnly cookies的值呢?我知道它与credentials:true一起工作,但是每次请求都会发送访问权和刷新令牌吗?也许响应拦截器已经过时了,因为当访问令牌无效时,我可以直接在服务器上检查刷新令牌
  • 如果我是正确的,那么当我通过授权标头(->first answer)发送令牌时,我不需要显式的CSRF保护。但当我切换到httpOnly Cookie时,我确实需要它,它带有
    with credentials:true
    标题,对吗
提前谢谢