【问题标题】:Where to store the JWT Client Credentials GrantJWT 客户端凭证授予的存储位置
【发布时间】:2018-05-24 20:14:36
【问题描述】:

我有一个 NodeJS Express 应用程序,它通过客户端凭据授予对 Auth 服务器进行身份验证。我收到的令牌用于从 API 加载数据。

在整个应用程序中存储令牌的最佳做法是什么?

请注意,JWT 不是特定于用户的,因为我的 Express 应用程序是客户端。

【问题讨论】:

    标签: javascript node.js express jwt openid-connect


    【解决方案1】:

    我会将它存储在内存中。通常我会写一个单例模块来处理它。

    auth.js:

    class Auth {
        getToken() {
            // check if we has token already and that token isn't expired
            if (this.token && !isExpired(this.token)) {
                return Promise.resolve(this.token);
            }
            // if not we call API for the new token then return the new token
            return asyncCallApiForToken();
        }
    }
    module.exports = new Auth();
    

    main.js

    const auth = require('./auth.js)
    
    auth.getToken()
        .then(token => {
            // we got token here
        }
    

    【讨论】:

      【解决方案2】:

      我会尽量避免持久化返回的令牌,只将其保存在内存中,因为客户端凭据授予使您能够相对轻松地获取新令牌,而无需用户交互。

      但如果这有问题,那么我会说:在客户端凭据旁边,因为客户端凭据至少与 JWT 令牌一样敏感。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-02-14
        • 1970-01-01
        • 2017-03-11
        • 2019-01-18
        • 1970-01-01
        • 2019-12-03
        • 2018-09-18
        • 2015-11-23
        相关资源
        最近更新 更多