How to use Basic auth in swagger ui v.3.0

后端 未结 3 1545
伪装坚强ぢ
伪装坚强ぢ 2020-12-31 15:47

in swagger ui 2.0 it was code

var basicAuth = new SwaggerClient.PasswordAuthorization(\"basicAuth\", username, password);
window.swaggerUi.api.clientAuthoriz         


        
3条回答
  •  青春惊慌失措
    2020-12-31 16:34

    I build an index.html with a simple form to fill user credentials to get a session id. Then redirect to swagger.html, for instance, and make some changes.

    Before window.onload:

    var orgFetch;
    
    window.setExtraHeader = function(sessionId) {
        var system = window.ui.getSystem();
    
        if(!system) return;
    
        if(!orgFetch) {
            orgFetch = system.fn.fetch;
        }
    
        system.fn.fetch = function(obj) {
            if(!obj) return;
    
            if(!obj.headers) {
                obj.headers = {};
            }
    
            obj.headers['sessionId'] = sessionId;
    
            return orgFetch(obj)
                .then(function(fetchRes) {
                    return fetchRes;
                });
        }
    
        system.specActions.download();
    }
    

    and then:

    window.ui = ui;
    window.setExtraHeader(localStorage.getItem("sessionId"));
    

    Source: https://github.com/swagger-api/swagger-ui/issues/2793

    Hope this helps.

提交回复
热议问题