From ca90fcb8fa35e6b04501bce8acbe61d087ed1a3b Mon Sep 17 00:00:00 2001 From: YunaiV <> Date: Sat, 2 Jan 2021 13:13:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E5=85=A5=20swagger=20+=20knife4j=20?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=EF=BC=8C=E5=AE=9E=E7=8E=B0=20API=20=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- http-client.env.json | 5 + pom.xml | 595 ++++++++++-------- .../config/SwaggerAutoConfiguration.java | 105 ++++ .../swagger/config/SwaggerProperties.java | 43 ++ .../framework/swagger/package-info.java | 6 + ... Spring Boot API 接口文档 Swagger 入门》.md | 1 + src/main/resources/application.yaml | 24 +- 7 files changed, 512 insertions(+), 267 deletions(-) create mode 100644 http-client.env.json create mode 100644 src/main/java/cn/iocoder/dashboard/framework/swagger/config/SwaggerAutoConfiguration.java create mode 100644 src/main/java/cn/iocoder/dashboard/framework/swagger/config/SwaggerProperties.java create mode 100644 src/main/java/cn/iocoder/dashboard/framework/swagger/package-info.java create mode 100644 src/main/java/cn/iocoder/dashboard/framework/swagger/《芋道 Spring Boot API 接口文档 Swagger 入门》.md diff --git a/http-client.env.json b/http-client.env.json new file mode 100644 index 000000000..b733618d3 --- /dev/null +++ b/http-client.env.json @@ -0,0 +1,5 @@ +{ + "local": { + "baseUrl": "http://127.0.0.1:8080" + } +} diff --git a/pom.xml b/pom.xml index b58f4c4fa..00701ba30 100644 --- a/pom.xml +++ b/pom.xml @@ -1,266 +1,329 @@ - - - 4.0.0 - - cn.iocoder - dashboard - 1.0.0 - - ruoyi - http://www.ruoyi.vip - 若依管理系统 - - - UTF-8 - UTF-8 - @ - 1.8 - ${java.version} - ${java.version} - 3.8.0 - - 2.4.1 - - - - - - - - - - - - - - - - - - - - - - 5.1.46 - 1.2.4 - 3.4.1 - - 1.16.14 - 1.3.0.Final - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - org.springframework.boot - spring-boot-dependencies - ${spring.boot.version} - pom - import - - - - - - - - - - - - - jar - - - - - - org.springframework.boot - spring-boot-starter-web - - - - - mysql - mysql-connector-java - ${mysql-connector-java.version} - - - - com.alibaba - druid-spring-boot-starter - ${druid.version} - - - - com.baomidou - mybatis-plus-boot-starter - ${mybatis-plus.version} - - - - - org.projectlombok - lombok - ${org.projectlombok.version} - - - - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - ${java.version} - ${java.version} - - - org.mapstruct - mapstruct-processor - ${org.mapstruct.version} - - - org.projectlombok - lombok - ${org.projectlombok.version} - - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - true - - - - - - - + + + 4.0.0 + + cn.iocoder + dashboard + 1.0.0 + + ruoyi + http://www.ruoyi.vip + 若依管理系统 + + + UTF-8 + UTF-8 + @ + 1.8 + ${java.version} + ${java.version} + 3.8.0 + + 2.4.1 + + + + + + + + + + + + + + + + + + + + 3.0.2 + + + 5.1.46 + 1.2.4 + 3.4.1 + 3.13.6 + + 1.16.14 + 1.3.0.Final + 0.9.1 + 1.2.75 + 5.5.6 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + org.springframework.boot + spring-boot-dependencies + ${spring.boot.version} + pom + import + + + + + + + + + + + + + jar + + + + + + org.springframework.boot + spring-boot-configuration-processor + true + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-security + + + + org.springframework.boot + spring-boot-starter-validation + + + + com.github.xiaoymin + knife4j-spring-boot-starter + ${knife4j.version} + + + + + mysql + mysql-connector-java + ${mysql-connector-java.version} + + + com.alibaba + druid-spring-boot-starter + ${druid.version} + + + com.baomidou + mybatis-plus-boot-starter + ${mybatis-plus.version} + + + + org.redisson + redisson-spring-boot-starter + ${redisson.version} + + + + + org.projectlombok + lombok + ${org.projectlombok.version} + + + + io.jsonwebtoken + jjwt + ${jjwt.version} + + + + com.alibaba + fastjson + ${fastjson.version} + + + + org.projectlombok + lombok + ${org.projectlombok.version} + + + + + eu.bitwalker + UserAgentUtils + 1.21 + + + + cn.hutool + hutool-core + ${hutool.version} + + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + ${java.version} + ${java.version} + + + org.mapstruct + mapstruct-processor + ${org.mapstruct.version} + + + org.projectlombok + lombok + ${org.projectlombok.version} + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + true + + + + + + + diff --git a/src/main/java/cn/iocoder/dashboard/framework/swagger/config/SwaggerAutoConfiguration.java b/src/main/java/cn/iocoder/dashboard/framework/swagger/config/SwaggerAutoConfiguration.java new file mode 100644 index 000000000..5b423c9f2 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/swagger/config/SwaggerAutoConfiguration.java @@ -0,0 +1,105 @@ +package cn.iocoder.dashboard.framework.swagger.config; + +import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpHeaders; +import springfox.documentation.builders.ApiInfoBuilder; +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.service.*; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spi.service.contexts.SecurityContext; +import springfox.documentation.spring.web.plugins.Docket; +import springfox.documentation.swagger2.annotations.EnableSwagger2; +import springfox.documentation.service.ApiKey; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static springfox.documentation.builders.RequestHandlerSelectors.basePackage; + +/** + * Swagger2 自动配置类 + * + * @author 芋道源码 + */ +@Configuration +@EnableSwagger2 +@EnableKnife4j +@ConditionalOnClass({Docket.class, ApiInfoBuilder.class}) +@ConditionalOnProperty(prefix = "yudao.swagger", value = "enable", matchIfMissing = true) +// 允许使用 swagger.enable=false 禁用 Swagger +@EnableConfigurationProperties(SwaggerProperties.class) +public class SwaggerAutoConfiguration { + + @Bean + @ConditionalOnMissingBean + public SwaggerProperties swaggerProperties() { + return new SwaggerProperties(); + } + + @Bean + public Docket createRestApi() { + SwaggerProperties properties = swaggerProperties(); + // 创建 Docket 对象 + return new Docket(DocumentationType.SWAGGER_2) + // 用来创建该 API 的基本信息,展示在文档的页面中(自定义展示的信息) + .apiInfo(apiInfo(properties)) + // 设置扫描指定 package 包下的 + .select() + .apis(basePackage(properties.getBasePackage())) + .paths(PathSelectors.any()) + .build() + .securitySchemes(securitySchemes()) + .securityContexts(securityContexts()) +// .pathMapping() TODO 芋艿:稍后解决,统一 api 前缀 + ; + } + + /** + * API 摘要信息 + */ + private static ApiInfo apiInfo(SwaggerProperties properties) { + return new ApiInfoBuilder() + .title(properties.getTitle()) + .description(properties.getDescription()) + .contact(new Contact(properties.getAuthor(), null, null)) + .version(properties.getVersion()) + .build(); + } + + /** + * 安全模式,这里配置通过请求头 Authorization 传递 token 参数 + */ + private static List securitySchemes() { + return Collections.singletonList(new ApiKey(HttpHeaders.AUTHORIZATION, "Authorization", "header")); + } + + /** + * 安全上下文 + * + * @see #securitySchemes() + * @see #authorizationScopes() + */ + private static List securityContexts() { + return Collections.singletonList(SecurityContext.builder() + .securityReferences(securityReferences()) + .forPaths(PathSelectors.regex("^(?!auth).*$")) + .build()); + } + + private static List securityReferences() { + return Collections.singletonList(new SecurityReference(HttpHeaders.AUTHORIZATION, authorizationScopes())); + } + + private static AuthorizationScope[] authorizationScopes() { + return new AuthorizationScope[]{new AuthorizationScope("global", "accessEverything")}; + } + +} diff --git a/src/main/java/cn/iocoder/dashboard/framework/swagger/config/SwaggerProperties.java b/src/main/java/cn/iocoder/dashboard/framework/swagger/config/SwaggerProperties.java new file mode 100644 index 000000000..6ee40c7d4 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/swagger/config/SwaggerProperties.java @@ -0,0 +1,43 @@ +package cn.iocoder.dashboard.framework.swagger.config; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; + +import javax.validation.constraints.NotEmpty; + +/** + * Swagger 配置属性 + * + * @author 芋道源码 + */ +@ConfigurationProperties("yudao.swagger") +@Data +public class SwaggerProperties { + + /** + * 标题 + */ + @NotEmpty(message = "标题不能为空") + private String title; + /** + * 描述 + */ + @NotEmpty(message = "描述不能为空") + private String description; + /** + * 作者 + */ + @NotEmpty(message = "作者不能为空") + private String author; + /** + * 版本 + */ + @NotEmpty(message = "版本不能为空") + private String version; + /** + * 扫描的包 + */ + @NotEmpty(message = "扫描的 package 不能为空") + private String basePackage; + +} diff --git a/src/main/java/cn/iocoder/dashboard/framework/swagger/package-info.java b/src/main/java/cn/iocoder/dashboard/framework/swagger/package-info.java new file mode 100644 index 000000000..2bce74a20 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/swagger/package-info.java @@ -0,0 +1,6 @@ +/** + * 基于 Swagger + Knife4j 实现 API 接口文档 + * + * @author 芋道源码 + */ +package cn.iocoder.dashboard.framework.swagger; diff --git a/src/main/java/cn/iocoder/dashboard/framework/swagger/《芋道 Spring Boot API 接口文档 Swagger 入门》.md b/src/main/java/cn/iocoder/dashboard/framework/swagger/《芋道 Spring Boot API 接口文档 Swagger 入门》.md new file mode 100644 index 000000000..906a67200 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/swagger/《芋道 Spring Boot API 接口文档 Swagger 入门》.md @@ -0,0 +1 @@ + diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 5d3a76219..d1cb9258f 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -1,7 +1,29 @@ spring: - # 数据源配置项 + # 数据源配置项 TODO 多数据源;TODO 监控配置 datasource: url: jdbc:mysql://127.0.1:3306/ruoyi-vue-pro?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT driver-class-name: com.mysql.jdbc.Driver username: root password: 123456 + # redis 配置 TODO 需要进行下,对接到 redisson + redis: + # 地址 + host: localhost + # 端口,默认为6379 + port: 6379 + # 数据库索引 + database: 0 + # 连接超时时间 + timeout: 10s + +# 芋道配置项,设置当前项目所有自定义的配置 +yudao: + security: + token-header: Authorization + token-secret: abcdefghijklmnopqrstuvwxyz + token-expires: 30m + swagger: + title: 管理后台 + description: 提供管理员管理的所有功能 + version: 1.0.0 + base-package: cn.iocoder.dashboard.modules