代码之家  ›  专栏  ›  技术社区  ›  Martin Staufcik

Swagger永久授权令牌

  •  1
  • Martin Staufcik  · 技术社区  · 7 年前

    1 回复  |  直到 7 年前
        1
  •  1
  •   Martin Staufcik    7 年前

    本地存储可用于保存授权令牌。

    localStorage.setItem('authKey', 'the authorization token')
    

    const ui = SwaggerUIBundle({
        url: "/swagger/v2/swagger.json",
        dom_id: '#swagger-ui',
        deepLinking: true,
        requestInterceptor: function (req) {
            var key = localStorage.getItem("authKey");
    
            if (key && key.trim() !== "") {
                req.headers.Authorization = 'Bearer ' + key;
                console.log('Authorized from authKey');
            }
        },
        presets: [
            SwaggerUIBundle.presets.apis,
            SwaggerUIStandalonePreset
        ],
        plugins: [
            SwaggerUIBundle.plugins.DownloadUrl
        ],
        layout: "StandaloneLayout",
    })
    
    window.ui = ui;