简体   繁体   English

如何使用 javascript 在 Swagger UI 3.0 版本中添加“承载”身份验证 header 而不是单击“授权”按钮

[英]How to add "bearer" auth header in Swagger UI 3.0 version using javascript instead of clicking "authorize" button

I am using Swagger UI 3.0 to call the Endpoint API's listed there.我正在使用 Swagger UI 3.0 来调用此处列出的端点 API。 And to call the api's, I have to add authentication in header.要调用 api,我必须在 header 中添加身份验证。 We had javascript to add the authentication to the swagger UI which adds the authentication globally to all API in the swagger UI for Swagger UI version of 2.0. We had javascript to add the authentication to the swagger UI which adds the authentication globally to all API in the swagger UI for Swagger UI version of 2.0. So this way, I don't have to click the "authorize" button in the swagger UI to add auth token in header.所以这样,我不必单击 swagger UI 中的“授权”按钮来在 header 中添加身份验证令牌。

But recently we moved to new swagger version of 3.0.但最近我们迁移到了新的 swagger 3.0 版本。 As a result, this javascript doesn't work anymore.结果,这个 javascript 不再工作了。

Is there any way to add header of Bearer auth using any javascript in Swagger UI 3.0 instead of clicking the "authorize" Button.有没有办法使用 Swagger UI 3.0 中的任何 javascript 添加承载身份验证的 header 而不是单击“授权”按钮。

I am asking this because It is really annoying to manually add the auth everytime I open the swagger UI to call API's.我问这个是因为每次打开 swagger UI 调用 API 时手动添加身份验证真的很烦人。

I don't want to click this below authorize button to add Auth header.我不想单击下面的授权按钮来添加 Auth header。 Instead I want to add this using some javascirpt.相反,我想使用一些 javascirpt 添加它。 在此处输入图像描述 在此处输入图像描述

In Swagger 3.0.0, globalOperationParameters is deprecated.Use globalRequestParameters in place of that.在 Swagger 3.0.0 中,不推荐使用 globalOperationParameters。使用 globalRequestParameters 代替它。 Below is the snippet.下面是片段。

@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo())
        .select()
        .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
        .paths(PathSelectors.any())
        .build()
        .globalRequestParameters(newArrayList(authorizationHeader()))
        .protocols(Sets.newHashSet("http", "https"));
}

private RequestParameter authorizationHeader() {

    return new RequestParameterBuilder()
        .name("Authorization")
        .description("Authorization header")
        .in(ParameterType.HEADER)
        .required(false)
        .build();
}

Pure javascript way to manipulate swaggerUI reactjs authorize form and set bearer token: (from browser console, tested in chrome)纯 javascript 方式来操作 swaggerUI reactjs 授权表单并设置不记名令牌:(来自浏览器控制台,在 chrome 中测试)

 let jwtToken = "YOUR_JWT_TOKEN"; //just token without 'Bearer ' prefix let openAuthFormButton = document.querySelector(".auth-wrapper.authorize.unlocked"); openAuthFormButton.click(); setTimeout(function() { let tokenInput = document.querySelector(".auth-container input"); let authButton = document.querySelector(".auth-btn-wrapper.modal-btn.auth"); let closeButton = document.querySelector("button.btn-done"); let nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, "value").set; nativeInputValueSetter.call(tokenInput, jwtToken); let inputEvent = new Event('input', { bubbles: true}); tokenInput.dispatchEvent(inputEvent); authButton.click(); closeButton.click(); }, 500);

This clicks 'Authorize' button which opens up authorize form, some wait is added (500ms) so form has time to open up and initialize form fields.单击“授权”按钮打开授权表单,添加一些等待(500 毫秒),以便表单有时间打开和初始化表单字段。 JWT token is then inserted into input field, and tricky part for me was to tell react that value was updated, for this you have to call set function directly (because react overrides it) and dispatch "input" event as described here https://stackoverflow.com/a/46012210/4695799然后将 JWT 令牌插入到输入字段中,对我来说棘手的部分是告诉 react 值已更新,为此您必须直接调用 set function(因为 react 会覆盖它)并按照此处所述Z5E0556C500A1C87B4 发送“输入”事件: /stackoverflow.com/a/46012210/4695799

After that it's walk in the park to perform click on 'Authorize' button and close the form.之后,它在公园里散步以执行点击“授权”按钮并关闭表格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM