小编mic*_*son的帖子

Swagger - 授权对带有文档的 url 的请求

我想知道 Swagger 中是否有一个选项可以授权对包含外部文档的 url 的请求。

我有以下配置,我感兴趣的是urls[1]部分

springdoc.swagger-ui.urls[0].url=/v3/api-docs
springdoc.swagger-ui.urls[0].name=default
springdoc.swagger-ui.urls[1].url=https://asdasd.blob.core.windows.net/my-api/docs.spec
springdoc.swagger-ui.urls[1].name=additional
Run Code Online (Sandbox Code Playgroud)

我可以从服务器(直接从机器上curl)访问文档,但是从Swagger(因此浏览器)我不能。我想知道是否有一个选项可以添加例如。Authorization Bearer xxxx对此请求或实际上使服务器发出请求,而不是客户端。

为了澄清 -我想从远程服务器获取带有 OpenAPI 定义的第二个文件,所以我正在谈论这个: 在此输入图像描述

不是Authorize部分 在此输入图像描述


我实施的解决方案:

海伦指出的可行 - 拦截请求并添加标头 - 但不幸的是,在我的情况下,还存在与存储的防火墙设置有关的问题。只有来自服务器的流量,而不是客户端(浏览器)通过,所以我:

  1. 将 url 设置为我的域中的某个内容
    springdoc.swagger-ui.urls[1].url=v3/api-docs/additional
  2. 为远程主机的 url 指定另一个属性
    mydomain.swagger-ui.additionalDocsUrl=...
  3. 实现了一个处理该 url 的自定义控制器 - 它作为服务器而不是客户端从远程获取 json,以便防火墙传递该请求
@Hidden
@RestController
@RequestMapping("/v3/api-docs/additional")
@RequiredArgsConstructor
public class AdditionalOpenApiController {

    @Value("${mydomain.swagger-ui.additionalDocsUrl}")
    private String additionalDocsUrl;

    @Cacheable(value = "ADDITIONAL_API_DOCS", unless = "#result != null")
    @GetMapping(produces = "application/json")
    public String getAdditionalDocs() {
        // in my case remote produces application/octet-stream …
Run Code Online (Sandbox Code Playgroud)

swagger spring-boot openapi springdoc

7
推荐指数
1
解决办法
1186
查看次数

使用 maven 插件在编译时从 Spring 应用程序源生成 OpenAPI 3.0 json/yaml

我想在编译时使用 Maven 插件从现有的 Spring(注意:不是引导)应用程序源生成 OpenApi 3.0 定义。

我在控制器类中设置io.swagger.v3.oas.annotations如下:

package com.acme.rest;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "Dummy Controller", description = "Dummy controller.")
@RestController
@RequestMapping("/api/v1/dummy")
public class DummyController {

    @Operation(summary = "dummy(). Does litrally nothing.")
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String doStuff() {
        return "dummy";
    }
}
Run Code Online (Sandbox Code Playgroud)

并尝试了swagger-maven-plugin

<plugin>
    <groupId>io.swagger.core.v3</groupId>
    <artifactId>swagger-maven-plugin</artifactId>
    <version>2.2.0</version>
    <configuration>
        <outputPath>${project.build.directory}/swagger-def</outputPath>
        <resourcePackages>com.acme</resourcePackages>
        <prettyPrint>true</prettyPrint>
    </configuration>
    <executions>
        <execution>
            <phase>compile</phase>
            <goals>
                <goal>resolve</goal>
            </goals>
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

但是除了openapi版本之外我什么也没得到。

mvn clean compile,产生:

{
  "openapi" : …
Run Code Online (Sandbox Code Playgroud)

java spring maven swagger

7
推荐指数
1
解决办法
4257
查看次数

SerialPort BaseStream ReadAsync - CancellationToken 从未取消?

我尝试以异步方式从串行端口读取数据,请记住操作所花费的时间不得超过指定的时间段。我使用的代码:

private async Task<int> Read(byte[] buffer, int offset, int length)
    {
        Console.WriteLine("Reading response to buffer of size {0}, offset {1}, max to cnt {2}", buffer.Length, offset, length);

        int totalBytesRead = 0;

        Console.WriteLine("Begin reading...");

        while (true)
        {
            Console.WriteLine("Re-entering read");

            int bytesRead = 0;

            using (CancellationTokenSource cts = new CancellationTokenSource(new TimeSpan(0, 0, 0, 0, 333))) // 333 ms
            {
                Console.WriteLine("Waiting for read to complete...");

                int tmpOffset = offset + totalBytesRead;
                int tmpLength = length - totalBytesRead;
                Console.WriteLine("tmpOffset: {0}, tmpLength: {1}", tmpOffset, tmpLength); …
Run Code Online (Sandbox Code Playgroud)

c# serial-port cancellationtokensource cancellation-token

3
推荐指数
1
解决办法
4341
查看次数