javascript Strapi JWT令牌生命周期?

kse8i1jr  于 2023-04-10  发布在  Java
关注(0)|答案(4)|浏览(182)

我找不到任何关于JWT令牌生命周期的信息,在Strapi CMS上多长时间有效?
我如何能自动刷新令牌时,它将是无效的?
我怎么做令牌验证,在我得到关于令牌生命周期结束的exeption之前?端点链接的名称是什么?

nwnhqdif

nwnhqdif1#

我很有信心,默认的持续时间是30天,它不能使用刷新令牌重新发行。
我认为这是一个开放的问题。您可以在这里查看更多信息:
https://github.com/strapi/strapi/issues/1676#issuecomment-409575253
它在未来更新的路线图上:
https://portal.productboard.com/strapi/1-public-roadmap/c/34-refresh-token-jwt

oogrdqng

oogrdqng3#

这里有一个目前的解决方案,刷新令牌需要认证。我目前的解决方案是在旧令牌到期后使用Axios Interceptor创建一个新令牌。

后台

  • 我已经覆盖了verify方法,所以我可以发送ignoreExpiration作为选项,否则verfiy会在token过期时抛出错误 *
const verify = (token) => {
  return new Promise(function(resolve, reject) {
    jwt.verify(
      token,
      _.get(strapi.plugins, ['users-permissions', 'config', 'jwtSecret']),
      {ignoreExpiration: true},
      function(err, tokenPayload = {}) {
        if (err) {
          return reject(new Error('Invalid token.'));
        }
        resolve(tokenPayload);
      }
    );
  });
}


module.exports = {
  refreshToken: async (ctx) => {
    const {token} = ctx.request.body;
    const payload = await verify(token);
    console.log(payload)
    return  strapi.plugins['users-permissions'].services.jwt.issue({id: payload.id})
  }
}
  • routes.json*
{
   "method": "POST",
   "path": "/refreshToken",
   "handler": "auth.refreshToken",
   "prefix": "",
   "config": {
     "policies": []
   }
 },

前端

  • 我已经使用axios-auth-refresh* 创建了一个拦截器,每当它检测到401错误时,它就会触发刷新令牌请求
import createAuthRefreshInterceptor from 'axios-auth-refresh';
import axios, { AxiosInstance } from "axios";

const refreshAuthLogic = (failedRequest:any) => axios.post(`${SERVER_URL}${REFRESH_TOKEN_URL}`, {token: failedRequest.response.config.headers['Authorization'].split(" ")[1]}).then(tokenRefreshResponse => {
    localStorage.setItem('token', tokenRefreshResponse.data);
    failedRequest.response.config.headers['Authorization'] = 'Bearer ' + tokenRefreshResponse.data;
    return Promise.resolve();
});

createAuthRefreshInterceptor(axiosInstance, refreshAuthLogic);
ogq8wdun

ogq8wdun4#

为此,你只需要去文件\node_modules@strapi\plugin-users-permissions\server\config.js找到行:jwt:{ expiresIn:'30 d',},并根据需要设置任意数量...例如(两小时):jwt:{ expiresIn:'2 h',},然后重新启动strapi服务器

相关问题