0

按照这里的文档 - https://micronaut-projects.github.io/micronaut-openapi/latest/guide/index.html

我将 build.gradle 配置为包含用于 swagger yaml 生成的编译时任务,如下所示 -

tasks.withType(JavaCompile){
options.fork = true
options.forkOptions.jvmArgs << '-Dmicronaut.openapi.views.spec=rapidoc.enabled=true,swagger-ui.enabled=true,swagger-ui.theme=flattop'
options.encoding = "UTF-8"
options.compilerArgs.add('-parameters')}

这就是我的 application.yaml 的样子——

micronaut:
   application:
    name: email-api
   server:
    port: 5655
    cors:
     enabled: true
   security:
     enabled: true
     token:
      jwt:
       enabled: true
       generator:
        accessTokenExpiration: 86400
       signatures:
        secret:
          generator:
            secret: Test@2020-Ok-Letus-chageit-later
   router:
    static-resources:
     swagger:
      paths: classpath:META-INF/swagger
      mapping: /swagger/**
     swagger-ui:
      paths: classpath:META-INF/swagger/views/swagger-ui
      mapping: /swagger-ui/**

就像文档说的那样,我还用 Application.java 进行了注释,如下所示-

import io.micronaut.runtime.Micronaut;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.info.Contact;
import io.swagger.v3.oas.annotations.info.Info;
import io.swagger.v3.oas.annotations.info.License;

@OpenAPIDefinition(
    info = @Info(
            title = "Email Service",
            version = "0.0",
            description = "Email Service API",
            license = @License(name = "Apache 2.0", url = "https://foo.bar"),
            contact = @Contact(url = "https://gigantic-server.com", name = "Fred", email = "Fred@gigagantic-server.com")
    )
)
public class Application {

public static void main(String[] args) {
    Micronaut.run(Application.class);
 }
}

完成所有这些之后,当我尝试打开http://localhost:5655/swagger/email-service-0.0.yaml时,它会打开生成的开放 API 规范 yaml。但是,如果我尝试打开http://localhost:5655/swagger-ui/,我会得到 404。请注意,如果我将 security 设置为false一切正常。

谁能帮我解决这个问题?

4

3 回答 3

1

我有同样的问题,但它适用于以下规则:

micronaut:
  security:
    enabled: true
    intercept-url-map:
      -
        pattern: /swagger-ui/**
        http-method: GET
        access:
          - isAnonymous() 

只需尝试两个**而不是一个。

于 2020-07-13T09:14:28.627 回答
0

从指南https://micronaut-projects.github.io/micronaut-security/latest/guide/

不需要包含 GET 方法

micronaut:
  security:
    enabled: true
    intercept-url-map:
      - pattern: /swagger-ui/**
        access:
          - isAnonymous()
于 2020-12-16T03:45:46.110 回答
-1

您可以在 swagger 端点上使用安全规则 https://micronaut-projects.github.io/micronaut-security/latest/guide/#interceptUrlMap

micronaut:
  security:
    enabled: true
    intercept-url-map:
      -
        pattern: /swagger-ui/*
        access:
          - isAnonymous() 
于 2020-06-11T19:55:53.650 回答