简体   繁体   中英

Swagger UI - OAuth 2.0 configuration with Micronaut 2.5.1 client ID and client secret in seperate configuration

Trying to do the swagger OAuth configuration in the latest version of Micronaut, as per the micronuat documentation https://micronaut-projects.github.io/micronaut-openapi/latest/guide/index.html#enableendpoints

swagger-ui.oauth2RedirectUrl
swagger-ui.oauth2.clientId
swagger-ui.oauth2.clientSecret
swagger-ui.oauth2.realm
swagger-ui.oauth2.appName
swagger-ui.oauth2.scopeSeparator
swagger-ui.oauth2.scopes
swagger-ui.oauth2.additionalQueryStringParams
swagger-ui.oauth2.useBasicAuthenticationWithAccessCodeGrant
swagger-ui.oauth2.usePkceWithAuthorizationCodeGrant

When setting any of those properties, Micronaut will generate not only a swagger-ui/index.html file, but also a swagger-ui/oauth2-redirect.html one

I can see it has created the file oauth2-redirect.html with below code

tasks.withType(JavaCompile).all {
    options.forkOptions.jvmArgs << '-Dmicronaut.openapi.views.spec=swagger-ui.enabled=true,swagger-ui.theme=flattop,swagger-ui.oauth2RedirectUrl=http://localhost:8080/swagger-ui/oauth2-redirect.html,swagger-ui.oauth2.clientId=myClientId,swagger-ui.oauth2.scopes=openid,swagger-ui.oauth2.usePkceWithAuthorizationCodeGrant=true'
}

For the oauth2.clientId, oauth2RedirectUrl and oauth2.clientSecret these values differs for each environment PROD, TEST, DEV, UAT. By setting the value like above code it is hard to configure for each enviroment. Is there any better way to do this?

In my project it was solved by creating different property files for each env and steering the compilation with parameters

tasks.withType(JavaCompile) {
  String openapiPropertyFile = 'openapi/openapi-dev.properties'
  if(project.hasProperty("openapiPropertyFile")){
      openapiPropertyFile = project.property('openapiPropertyFile')
  }
  options.fork = true
  options.forkOptions.jvmArgs << "-Dmicronaut.openapi.config.file=$openapiPropertyFile"
}

and then you build it like this

$ sh ./gradlew clean jib -PopenapiPropertyFile=openapi/openapi-uat.properties -PimageSuffix=-uat

Although I'd like to know how to do this post compilation because in my case it forces creation of separate docker images where I'd like to have one and mount those properties afterwards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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