From 1e4749bfdbd08fa49426a93676bfcf2e7d15b262 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=9D=E5=85=88=E7=91=9E?= <1490493387@qq.com> Date: Wed, 27 Sep 2023 18:22:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20knife4j=20v4.3=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E7=BD=91=E5=85=B3=E8=81=9A=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../youlai/mall/oms/config/SwaggerConfig.java | 43 ++++++++++------- .../youlai/mall/pms/config/SwaggerConfig.java | 44 ++++++++++------- .../youlai/mall/sms/config/SwaggerConfig.java | 44 ++++++++++------- .../youlai/mall/ums/config/SwaggerConfig.java | 45 +++++++++++------- .../com/youlai/auth/config/SwaggerConfig.java | 47 +++++++++++-------- .../youlai/system/config/SwaggerConfig.java | 45 +++++++++++------- 6 files changed, 165 insertions(+), 103 deletions(-) diff --git a/mall-oms/oms-boot/src/main/java/com/youlai/mall/oms/config/SwaggerConfig.java b/mall-oms/oms-boot/src/main/java/com/youlai/mall/oms/config/SwaggerConfig.java index 54e323723..c8c54b870 100644 --- a/mall-oms/oms-boot/src/main/java/com/youlai/mall/oms/config/SwaggerConfig.java +++ b/mall-oms/oms-boot/src/main/java/com/youlai/mall/oms/config/SwaggerConfig.java @@ -4,8 +4,11 @@ import io.swagger.v3.oas.models.Components; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.info.Info; import io.swagger.v3.oas.models.info.License; +import io.swagger.v3.oas.models.security.OAuthFlows; +import io.swagger.v3.oas.models.security.Scopes; import io.swagger.v3.oas.models.security.SecurityScheme; import org.springdoc.core.models.GroupedOpenApi; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -20,6 +23,12 @@ import org.springframework.context.annotation.Configuration; @Configuration public class SwaggerConfig { + /** + * 认证服务地址 + */ + @Value("${knife4j.oauth2.token-url}") + private String tokenUrl ; + /** * 接口信息 */ @@ -28,31 +37,33 @@ public class SwaggerConfig { return new OpenAPI() .components(new Components() .addSecuritySchemes("Authorization", - new SecurityScheme().type(SecurityScheme.Type.HTTP) - .scheme("bearer").bearerFormat("JWT") + new SecurityScheme() + .name("Authorization") + .type(SecurityScheme.Type.OAUTH2) + .flows(new OAuthFlows() + .password( + new io.swagger.v3.oas.models.security.OAuthFlow() + .tokenUrl(tokenUrl) + .refreshUrl(tokenUrl) + .scopes( + new Scopes() + .addString("all", "全部权限") + ) + ) + ) + .scheme("bearer") + .in(SecurityScheme.In.HEADER) + .bearerFormat("JWT") ) ) .info(new Info() .title("订单服务") .version("3.0.0") - .description("订单服务接口文档") + .description("订单、购物车等接口") .license(new License().name("Apache 2.0") .url("https://www.apache.org/licenses/LICENSE-2.0")) ); } - /** - * 接口分组 - */ - @Bean - public GroupedOpenApi omsApi() { - String[] paths = {"/**"}; - String[] packagesToScan = {"com.youlai.mall.oms.controller"}; - return GroupedOpenApi.builder() - .group("订单服务") - .packagesToScan(packagesToScan) - .pathsToMatch(paths) - .build(); - } } diff --git a/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/config/SwaggerConfig.java b/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/config/SwaggerConfig.java index 95cdfd1bc..8dfdfa993 100644 --- a/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/config/SwaggerConfig.java +++ b/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/config/SwaggerConfig.java @@ -4,8 +4,11 @@ import io.swagger.v3.oas.models.Components; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.info.Info; import io.swagger.v3.oas.models.info.License; +import io.swagger.v3.oas.models.security.OAuthFlows; +import io.swagger.v3.oas.models.security.Scopes; import io.swagger.v3.oas.models.security.SecurityScheme; import org.springdoc.core.models.GroupedOpenApi; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -20,6 +23,12 @@ import org.springframework.context.annotation.Configuration; @Configuration public class SwaggerConfig { + /** + * 认证服务地址 + */ + @Value("${knife4j.oauth2.token-url}") + private String tokenUrl ; + /** * 接口信息 */ @@ -28,31 +37,34 @@ public class SwaggerConfig { return new OpenAPI() .components(new Components() .addSecuritySchemes("Authorization", - new SecurityScheme().type(SecurityScheme.Type.HTTP) - .scheme("bearer").bearerFormat("JWT") + new SecurityScheme() + .name("Authorization") + .type(SecurityScheme.Type.OAUTH2) + .flows(new OAuthFlows() + .password( + new io.swagger.v3.oas.models.security.OAuthFlow() + .tokenUrl(tokenUrl) + .refreshUrl(tokenUrl) + .scopes( + new Scopes() + .addString("all", "全部权限") + ) + ) + ) + .scheme("bearer") + .in(SecurityScheme.In.HEADER) + .bearerFormat("JWT") ) ) .info(new Info() .title("商品服务") .version("3.0.0") - .description("商品管理、库存、分类、品牌、规格、参数等接口") + .description("库存、分类、品牌、规格、参数等接口") .license(new License().name("Apache 2.0") .url("https://www.apache.org/licenses/LICENSE-2.0")) ); } - /** - * 接口分组 - */ - @Bean - public GroupedOpenApi orderApi() { - String[] paths = {"/**"}; - String[] packagesToScan = {"com.youlai.mall.pms.controller"}; - return GroupedOpenApi.builder() - .group("商品服务") - .packagesToScan(packagesToScan) - .pathsToMatch(paths) - .build(); - } + } diff --git a/mall-sms/sms-boot/src/main/java/com/youlai/mall/sms/config/SwaggerConfig.java b/mall-sms/sms-boot/src/main/java/com/youlai/mall/sms/config/SwaggerConfig.java index b3aff3156..f0403d364 100644 --- a/mall-sms/sms-boot/src/main/java/com/youlai/mall/sms/config/SwaggerConfig.java +++ b/mall-sms/sms-boot/src/main/java/com/youlai/mall/sms/config/SwaggerConfig.java @@ -4,8 +4,11 @@ import io.swagger.v3.oas.models.Components; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.info.Info; import io.swagger.v3.oas.models.info.License; +import io.swagger.v3.oas.models.security.OAuthFlows; +import io.swagger.v3.oas.models.security.Scopes; import io.swagger.v3.oas.models.security.SecurityScheme; import org.springdoc.core.models.GroupedOpenApi; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -20,6 +23,12 @@ import org.springframework.context.annotation.Configuration; @Configuration public class SwaggerConfig { + /** + * 认证服务地址 + */ + @Value("${knife4j.oauth2.token-url}") + private String tokenUrl ; + /** * 接口信息 */ @@ -28,31 +37,32 @@ public class SwaggerConfig { return new OpenAPI() .components(new Components() .addSecuritySchemes("Authorization", - new SecurityScheme().type(SecurityScheme.Type.HTTP) - .scheme("bearer").bearerFormat("JWT") + new SecurityScheme() + .name("Authorization") + .type(SecurityScheme.Type.OAUTH2) + .flows(new OAuthFlows() + .password( + new io.swagger.v3.oas.models.security.OAuthFlow() + .tokenUrl(tokenUrl) + .refreshUrl(tokenUrl) + .scopes( + new Scopes() + .addString("all", "全部权限") + ) + ) + ) + .scheme("bearer") + .in(SecurityScheme.In.HEADER) + .bearerFormat("JWT") ) ) .info(new Info() .title("营销服务") .version("3.0.0") - .description("首页广告等接口") + .description("广告、优惠券等接口") .license(new License().name("Apache 2.0") .url("https://www.apache.org/licenses/LICENSE-2.0")) ); } - /** - * 接口分组 - */ - @Bean - public GroupedOpenApi smsApi() { - String[] paths = {"/**"}; - String[] packagesToScan = {"com.youlai.mall.sms.controller"}; - return GroupedOpenApi.builder() - .group("营销服务") - .packagesToScan(packagesToScan) - .pathsToMatch(paths) - .build(); - } - } diff --git a/mall-ums/ums-boot/src/main/java/com/youlai/mall/ums/config/SwaggerConfig.java b/mall-ums/ums-boot/src/main/java/com/youlai/mall/ums/config/SwaggerConfig.java index 3374bf6cf..a1442f631 100644 --- a/mall-ums/ums-boot/src/main/java/com/youlai/mall/ums/config/SwaggerConfig.java +++ b/mall-ums/ums-boot/src/main/java/com/youlai/mall/ums/config/SwaggerConfig.java @@ -4,8 +4,11 @@ import io.swagger.v3.oas.models.Components; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.info.Info; import io.swagger.v3.oas.models.info.License; +import io.swagger.v3.oas.models.security.OAuthFlows; +import io.swagger.v3.oas.models.security.Scopes; import io.swagger.v3.oas.models.security.SecurityScheme; import org.springdoc.core.models.GroupedOpenApi; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -20,6 +23,13 @@ import org.springframework.context.annotation.Configuration; @Configuration public class SwaggerConfig { + + /** + * 认证服务地址 + */ + @Value("${knife4j.oauth2.token-url}") + private String tokenUrl ; + /** * 接口信息 */ @@ -28,31 +38,32 @@ public class SwaggerConfig { return new OpenAPI() .components(new Components() .addSecuritySchemes("Authorization", - new SecurityScheme().type(SecurityScheme.Type.HTTP) - .scheme("bearer").bearerFormat("JWT") + new SecurityScheme() + .name("Authorization") + .type(SecurityScheme.Type.OAUTH2) + .flows(new OAuthFlows() + .password( + new io.swagger.v3.oas.models.security.OAuthFlow() + .tokenUrl(tokenUrl) + .refreshUrl(tokenUrl) + .scopes( + new Scopes() + .addString("all", "全部权限") + ) + ) + ) + .scheme("bearer") + .in(SecurityScheme.In.HEADER) + .bearerFormat("JWT") ) ) .info(new Info() .title("会员服务") .version("3.0.0") - .description("会员管理、地址接口") + .description("会员、地址等接口") .license(new License().name("Apache 2.0") .url("https://www.apache.org/licenses/LICENSE-2.0")) ); } - /** - * 接口分组 - */ - @Bean - public GroupedOpenApi orderApi() { - String[] paths = {"/**"}; - String[] packagesToScan = {"com.youlai.mall.ums.controller"}; - return GroupedOpenApi.builder() - .group("会员服务") - .packagesToScan(packagesToScan) - .pathsToMatch(paths) - .build(); - } - } diff --git a/youlai-auth/src/main/java/com/youlai/auth/config/SwaggerConfig.java b/youlai-auth/src/main/java/com/youlai/auth/config/SwaggerConfig.java index 4498f8a04..39b124dcb 100644 --- a/youlai-auth/src/main/java/com/youlai/auth/config/SwaggerConfig.java +++ b/youlai-auth/src/main/java/com/youlai/auth/config/SwaggerConfig.java @@ -4,15 +4,15 @@ import io.swagger.v3.oas.models.Components; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.info.Info; import io.swagger.v3.oas.models.info.License; +import io.swagger.v3.oas.models.security.OAuthFlows; +import io.swagger.v3.oas.models.security.Scopes; import io.swagger.v3.oas.models.security.SecurityScheme; -import org.springdoc.core.models.GroupedOpenApi; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * Swagger 配置 - *
- * Spring Doc FAQ: https://springdoc.org/#faq * * @author haoxr * @since 2023/2/17 @@ -20,6 +20,12 @@ import org.springframework.context.annotation.Configuration; @Configuration public class SwaggerConfig { + /** + * 认证服务地址 + */ + @Value("${knife4j.oauth2.token-url}") + private String tokenUrl ; + /** * 接口信息 */ @@ -28,31 +34,32 @@ public class SwaggerConfig { return new OpenAPI() .components(new Components() .addSecuritySchemes("Authorization", - new SecurityScheme().type(SecurityScheme.Type.HTTP) - .scheme("bearer").bearerFormat("JWT") + new SecurityScheme() + .name("Authorization") + .type(SecurityScheme.Type.OAUTH2) + .flows(new OAuthFlows() + .password( + new io.swagger.v3.oas.models.security.OAuthFlow() + .tokenUrl(tokenUrl) + .refreshUrl(tokenUrl) + .scopes( + new Scopes() + .addString("all", "全部权限") + ) + ) + ) + .scheme("bearer") + .in(SecurityScheme.In.HEADER) + .bearerFormat("JWT") ) ) .info(new Info() .title("认证中心") .version("3.0.0") - .description("认证、注销、获取验签公钥接口") + .description("认证接口") .license(new License().name("Apache 2.0") .url("https://www.apache.org/licenses/LICENSE-2.0")) ); } - /** - * 认证中心接口 - */ - @Bean - public GroupedOpenApi authApi() { - String[] paths = {"/**"}; - String[] packagesToScan = {"com.youlai.auth.controller"}; - return GroupedOpenApi.builder() - .group("认证中心") - .packagesToScan(packagesToScan) - .pathsToMatch(paths) - .build(); - } - } diff --git a/youlai-system/system-boot/src/main/java/com/youlai/system/config/SwaggerConfig.java b/youlai-system/system-boot/src/main/java/com/youlai/system/config/SwaggerConfig.java index c539d21b3..2bc2b3496 100644 --- a/youlai-system/system-boot/src/main/java/com/youlai/system/config/SwaggerConfig.java +++ b/youlai-system/system-boot/src/main/java/com/youlai/system/config/SwaggerConfig.java @@ -4,8 +4,11 @@ import io.swagger.v3.oas.models.Components; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.info.Info; import io.swagger.v3.oas.models.info.License; +import io.swagger.v3.oas.models.security.OAuthFlows; +import io.swagger.v3.oas.models.security.Scopes; import io.swagger.v3.oas.models.security.SecurityScheme; import org.springdoc.core.models.GroupedOpenApi; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -20,6 +23,12 @@ import org.springframework.context.annotation.Configuration; @Configuration public class SwaggerConfig { + /** + * 认证服务地址 + */ + @Value("${knife4j.oauth2.token-url}") + private String tokenUrl ; + /** * 接口信息 */ @@ -28,31 +37,33 @@ public class SwaggerConfig { return new OpenAPI() .components(new Components() .addSecuritySchemes("Authorization", - new SecurityScheme().type(SecurityScheme.Type.HTTP) - .scheme("bearer").bearerFormat("JWT") + new SecurityScheme() + .name("Authorization") + .type(SecurityScheme.Type.OAUTH2) + .flows(new OAuthFlows() + .password( + new io.swagger.v3.oas.models.security.OAuthFlow() + .tokenUrl(tokenUrl) + .refreshUrl(tokenUrl) + .scopes( + new Scopes() + .addString("all", "全部权限") + ) + ) + ) + .scheme("bearer") + .in(SecurityScheme.In.HEADER) + .bearerFormat("JWT") ) ) .info(new Info() .title("系统服务") - .version("2.0.0") - .description("系统服务接口文档") + .version("3.0.0") + .description("用户、角色、菜单、部门、字典等接口") .license(new License().name("Apache 2.0") .url("https://www.apache.org/licenses/LICENSE-2.0")) ); } - /** - * 接口分组-系统接口 - */ - @Bean - public GroupedOpenApi systemApi() { - String[] paths = {"/**"}; - String[] packagesToScan = {"com.youlai.system.controller"}; - return GroupedOpenApi.builder() - .group("系统服务") - .packagesToScan(packagesToScan) - .pathsToMatch(paths) - .build(); - } }