如何编程向swagger.yaml添加securitySchema并按环境配置授权URL
实现方案
你可以通过注入环境配置+动态修改/编程式构造Swagger配置的方式实现多环境授权URL适配,不需要修改静态文件重新打包,以下是两种常用实现方式:
方案1:保留现有静态swagger.yaml,动态替换授权配置
适合不想改动现有静态API定义的场景:
- 首先在多环境配置文件中添加不同环境的授权地址,示例(Spring Boot环境):
# 开发环境配置 application-dev.yml swagger: auth-url: https://dev-auth.example.com/oauth/authorize --- # 测试环境配置 application-test.yml swagger: auth-url: https://test-auth.example.com/oauth/authorize --- # 生产环境配置 application-prod.yml swagger: auth-url: https://prod-auth.example.com/oauth/authorize
- 将原来放在静态目录的
swagger.yaml改名为template-swagger.yaml放到resources目录下作为模板,新增接口动态读取模板替换授权URL后返回:
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpHeaders; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.io.IOException; import java.io.InputStream; @RestController public class SwaggerYamlController { @Value("${swagger.auth-url}") private String authUrl; private final ObjectMapper yamlMapper = new YAMLMapper(); @GetMapping("/swagger.yaml") public ResponseEntity<String> getModifiedSwaggerYaml() throws IOException { // 读取模板yaml InputStream templateStream = getClass().getResourceAsStream("/template-swagger.yaml"); JsonNode rootNode = yamlMapper.readTree(templateStream); // 替换security schema的授权地址,此处请替换为你自己的schema名称 JsonNode components = rootNode.get("components"); JsonNode securitySchemes = components.get("securitySchemes"); ((com.fasterxml.jackson.databind.node.ObjectNode) securitySchemes.get("你的securitySchema名称")) .put("authorizationUrl", authUrl); // 返回修改后的yaml String modifiedYaml = yamlMapper.writerWithDefaultPrettyPrinter().writeValueAsString(rootNode); return ResponseEntity.ok() .header(HttpHeaders.CONTENT_TYPE, "application/yaml") .body(modifiedYaml); } }
- 你原有
SwaggerResourcesProvider的配置无需修改,不同环境启动时激活对应profile即可自动适配授权URL,也可以直接通过启动参数-Dswagger.auth-url=自定义地址临时覆盖配置。
方案2:完全编程式构造OpenAPI配置,弃用静态yaml
适合愿意将API定义改为代码维护的场景,适配性更强:
如果使用SpringDoc OpenAPI(推荐,比Springfox维护更活跃),直接注入配置构造OpenAPI对象即可:
import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.info.Info; import io.swagger.v3.oas.models.security.*; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class OpenApiConfig { @Value("${swagger.auth-url}") private String authUrl; @Bean public OpenAPI customOpenAPI() { return new OpenAPI() .info(new Info().title("接口文档").version("3.0")) .components(new Components() .addSecuritySchemes("oauth2", new SecurityScheme() .type(SecurityScheme.Type.OAUTH2) .flows(new OAuthFlows() .authorizationCode(new OAuthFlow() .authorizationUrl(authUrl) .scopes(new Scopes() .addString("read", "读权限") .addString("write", "写权限")))) )); } }
如果使用Springfox 3.x,逻辑类似,注入配置的authUrl后将构造好的SecurityScheme加入Docket配置即可。
内容的提问来源于stack exchange,提问作者karolb11
相关产品推荐
相关产品推荐

