From f71bf701f15721d21b7848daea7b5e5bff94efd6 Mon Sep 17 00:00:00 2001 From: dataprince Date: Fri, 28 Jul 2023 16:49:19 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9B=86=E6=88=90SpringDoc=EF=BC=8C=E4=BB=A3?= =?UTF-8?q?=E6=9B=BFspringfox?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 62 ++++-- ruoyi-admin/pom.xml | 20 +- .../web/controller/tool/TestController.java | 183 ------------------ .../ruoyi/web/core/config/SwaggerConfig.java | 125 ------------ .../src/main/resources/application-dev.yml | 5 + .../src/main/resources/application.yml | 23 ++- ruoyi-common/ruoyi-common-springdoc/pom.xml | 20 ++ .../common/springdoc/SpringDocConfig.java | 45 +++++ .../framework/config/SecurityConfig.java | 3 +- ruoyi-ui/src/views/tool/swagger/index.vue | 2 +- 10 files changed, 144 insertions(+), 344 deletions(-) delete mode 100644 ruoyi-admin/src/main/java/com/ruoyi/web/controller/tool/TestController.java delete mode 100644 ruoyi-admin/src/main/java/com/ruoyi/web/core/config/SwaggerConfig.java create mode 100644 ruoyi-common/ruoyi-common-springdoc/src/main/java/com/ruoyi/common/springdoc/SpringDocConfig.java diff --git a/pom.xml b/pom.xml index 99962e2..3b3f591 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,6 @@ 3.1.1 5.0.1 1.21 - 3.0.0 2.3.3 1.4.6 5.3.3 @@ -37,6 +36,11 @@ 6.0.0 32.1.1-jre 1.5.0 + 2.1.0 + 2.1.0 + 0.15.0 + + 3.11.0 @@ -111,19 +115,6 @@ ${oshi.version} - - - io.springfox - springfox-boot-starter - ${swagger.version} - - - io.swagger - swagger-models - - - - commons-io @@ -173,12 +164,39 @@ ${kaptcha.version} + com.google.guava guava ${guava.version} + + + org.springdoc + springdoc-openapi-starter-webmvc-ui + ${springdoc.version} + + + + org.springdoc + springdoc-openapi-starter-webmvc-api + ${springdoc.version} + + + + + org.springdoc + springdoc-openapi-starter-common + ${springdoc-openapi-starter-common.version} + + + + com.github.therapi + therapi-runtime-javadoc + ${therapi-runtime-javadoc.version} + + com.ruoyi @@ -214,6 +232,13 @@ ${revision} + + + com.ruoyi + ruoyi-common-springdoc + ${revision} + + com.ruoyi @@ -237,11 +262,18 @@ org.apache.maven.plugins maven-compiler-plugin - 3.1 + ${maven-compiler-plugin.verison} ${java.version} ${java.version} ${project.build.sourceEncoding} + + + com.github.therapi + therapi-runtime-javadoc-scribe + ${therapi-runtime-javadoc.version} + + diff --git a/ruoyi-admin/pom.xml b/ruoyi-admin/pom.xml index 46962ce..587a65e 100644 --- a/ruoyi-admin/pom.xml +++ b/ruoyi-admin/pom.xml @@ -24,19 +24,6 @@ true - - - io.springfox - springfox-boot-starter - - - - - io.swagger - swagger-models - 1.6.2 - - mysql @@ -67,6 +54,13 @@ ruoyi-generator + + + com.ruoyi + ruoyi-common-springdoc + ${revision} + + com.ruoyi diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/tool/TestController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/tool/TestController.java deleted file mode 100644 index 0712a80..0000000 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/tool/TestController.java +++ /dev/null @@ -1,183 +0,0 @@ -package com.ruoyi.web.controller.tool; - -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.PutMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; -import com.ruoyi.common.core.core.controller.BaseController; -import com.ruoyi.common.core.core.domain.R; -import com.ruoyi.common.core.utils.StringUtils; -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiImplicitParam; -import io.swagger.annotations.ApiImplicitParams; -import io.swagger.annotations.ApiModel; -import io.swagger.annotations.ApiModelProperty; -import io.swagger.annotations.ApiOperation; - -/** - * swagger 用户测试方法 - * - * @author ruoyi - */ -@Api("用户信息管理") -@RestController -@RequestMapping("/test/user") -public class TestController extends BaseController -{ - private final static Map users = new LinkedHashMap(); - { - users.put(1, new UserEntity(1, "admin", "admin123", "15888888888")); - users.put(2, new UserEntity(2, "ry", "admin123", "15666666666")); - } - - @ApiOperation("获取用户列表") - @GetMapping("/list") - public R> userList() - { - List userList = new ArrayList(users.values()); - return R.ok(userList); - } - - @ApiOperation("获取用户详细") - @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path", dataTypeClass = Integer.class) - @GetMapping("/{userId}") - public R getUser(@PathVariable Integer userId) - { - if (!users.isEmpty() && users.containsKey(userId)) - { - return R.ok(users.get(userId)); - } - else - { - return R.fail("用户不存在"); - } - } - - @ApiOperation("新增用户") - @ApiImplicitParams({ - @ApiImplicitParam(name = "userId", value = "用户id", dataType = "Integer", dataTypeClass = Integer.class), - @ApiImplicitParam(name = "username", value = "用户名称", dataType = "String", dataTypeClass = String.class), - @ApiImplicitParam(name = "password", value = "用户密码", dataType = "String", dataTypeClass = String.class), - @ApiImplicitParam(name = "mobile", value = "用户手机", dataType = "String", dataTypeClass = String.class) - }) - @PostMapping("/save") - public R save(UserEntity user) - { - if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId())) - { - return R.fail("用户ID不能为空"); - } - users.put(user.getUserId(), user); - return R.ok(); - } - - @ApiOperation("更新用户") - @PutMapping("/update") - public R update(@RequestBody UserEntity user) - { - if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId())) - { - return R.fail("用户ID不能为空"); - } - if (users.isEmpty() || !users.containsKey(user.getUserId())) - { - return R.fail("用户不存在"); - } - users.remove(user.getUserId()); - users.put(user.getUserId(), user); - return R.ok(); - } - - @ApiOperation("删除用户信息") - @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path", dataTypeClass = Integer.class) - @DeleteMapping("/{userId}") - public R delete(@PathVariable Integer userId) - { - if (!users.isEmpty() && users.containsKey(userId)) - { - users.remove(userId); - return R.ok(); - } - else - { - return R.fail("用户不存在"); - } - } -} - -@ApiModel(value = "UserEntity", description = "用户实体") -class UserEntity -{ - @ApiModelProperty("用户ID") - private Integer userId; - - @ApiModelProperty("用户名称") - private String username; - - @ApiModelProperty("用户密码") - private String password; - - @ApiModelProperty("用户手机") - private String mobile; - - public UserEntity() - { - - } - - public UserEntity(Integer userId, String username, String password, String mobile) - { - this.userId = userId; - this.username = username; - this.password = password; - this.mobile = mobile; - } - - public Integer getUserId() - { - return userId; - } - - public void setUserId(Integer userId) - { - this.userId = userId; - } - - public String getUsername() - { - return username; - } - - public void setUsername(String username) - { - this.username = username; - } - - public String getPassword() - { - return password; - } - - public void setPassword(String password) - { - this.password = password; - } - - public String getMobile() - { - return mobile; - } - - public void setMobile(String mobile) - { - this.mobile = mobile; - } -} diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/core/config/SwaggerConfig.java b/ruoyi-admin/src/main/java/com/ruoyi/web/core/config/SwaggerConfig.java deleted file mode 100644 index 2a62095..0000000 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/core/config/SwaggerConfig.java +++ /dev/null @@ -1,125 +0,0 @@ -package com.ruoyi.web.core.config; - -import java.util.ArrayList; -import java.util.List; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import com.ruoyi.common.core.config.RuoYiConfig; -import io.swagger.annotations.ApiOperation; -import io.swagger.models.auth.In; -import springfox.documentation.builders.ApiInfoBuilder; -import springfox.documentation.builders.PathSelectors; -import springfox.documentation.builders.RequestHandlerSelectors; -import springfox.documentation.service.ApiInfo; -import springfox.documentation.service.ApiKey; -import springfox.documentation.service.AuthorizationScope; -import springfox.documentation.service.Contact; -import springfox.documentation.service.SecurityReference; -import springfox.documentation.service.SecurityScheme; -import springfox.documentation.spi.DocumentationType; -import springfox.documentation.spi.service.contexts.SecurityContext; -import springfox.documentation.spring.web.plugins.Docket; - -/** - * Swagger2的接口配置 - * - * @author ruoyi - */ -@Configuration -public class SwaggerConfig -{ - /** 系统基础配置 */ - @Autowired - private RuoYiConfig ruoyiConfig; - - /** 是否开启swagger */ - @Value("${swagger.enabled}") - private boolean enabled; - - /** 设置请求的统一前缀 */ - @Value("${swagger.pathMapping}") - private String pathMapping; - - /** - * 创建API - */ - @Bean - public Docket createRestApi() - { - return new Docket(DocumentationType.OAS_30) - // 是否启用Swagger - .enable(enabled) - // 用来创建该API的基本信息,展示在文档的页面中(自定义展示的信息) - .apiInfo(apiInfo()) - // 设置哪些接口暴露给Swagger展示 - .select() - // 扫描所有有注解的api,用这种方式更灵活 - .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) - // 扫描指定包中的swagger注解 - // .apis(RequestHandlerSelectors.basePackage("com.ruoyi.project.tool.swagger")) - // 扫描所有 .apis(RequestHandlerSelectors.any()) - .paths(PathSelectors.any()) - .build() - /* 设置安全模式,swagger可以设置访问token */ - .securitySchemes(securitySchemes()) - .securityContexts(securityContexts()) - .pathMapping(pathMapping); - } - - /** - * 安全模式,这里指定token通过Authorization头请求头传递 - */ - private List securitySchemes() - { - List apiKeyList = new ArrayList<>(); - apiKeyList.add(new ApiKey("Authorization", "Authorization", In.HEADER.toValue())); - return apiKeyList; - } - - /** - * 安全上下文 - */ - private List securityContexts() - { - List securityContexts = new ArrayList<>(); - securityContexts.add( - SecurityContext.builder() - .securityReferences(defaultAuth()) - .operationSelector(o -> o.requestMappingPattern().matches("/.*")) - .build()); - return securityContexts; - } - - /** - * 默认的安全上引用 - */ - private List defaultAuth() - { - AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); - AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; - authorizationScopes[0] = authorizationScope; - List securityReferences = new ArrayList<>(); - securityReferences.add(new SecurityReference("Authorization", authorizationScopes)); - return securityReferences; - } - - /** - * 添加摘要信息 - */ - private ApiInfo apiInfo() - { - // 用ApiInfoBuilder进行定制 - return new ApiInfoBuilder() - // 设置标题 - .title("标题:若依管理系统_接口文档") - // 描述 - .description("描述:用于管理集团旗下公司的人员信息,具体包括XXX,XXX模块...") - // 作者信息 - .contact(new Contact(ruoyiConfig.getName(), null, null)) - // 版本 - .version("版本号:" + ruoyiConfig.getVersion()) - .build(); - } -} diff --git a/ruoyi-admin/src/main/resources/application-dev.yml b/ruoyi-admin/src/main/resources/application-dev.yml index b85b457..ea3125d 100644 --- a/ruoyi-admin/src/main/resources/application-dev.yml +++ b/ruoyi-admin/src/main/resources/application-dev.yml @@ -25,7 +25,12 @@ mybatis-flex: max-lifetime: 1800000 # 数据库连接超时时间,默认30秒 connection-timeout: 30000 + # 校验超时时间 + validationTimeout: 5000 + # 多久检查一次连接的活性 + keepaliveTime: 30000 # 连接测试query connection-test-query: SELECT 1 + diff --git a/ruoyi-admin/src/main/resources/application.yml b/ruoyi-admin/src/main/resources/application.yml index 7106ce2..67f25d9 100644 --- a/ruoyi-admin/src/main/resources/application.yml +++ b/ruoyi-admin/src/main/resources/application.yml @@ -125,12 +125,23 @@ pagehelper: supportMethodsArguments: true params: count=countSql -# Swagger配置 -swagger: - # 是否开启swagger - enabled: true - # 请求前缀 - pathMapping: /dev-api +# SpringDoc配置 +springdoc: + packages-to-scan: ##需要扫描的包,可以配置多个 + - com.ruoyi.demo.* + paths-to-exclude: ##配置不包含在swagger文档中的api + - /api/test/** + - /api/mockito/data + swagger-ui: + enabled: true #开启/禁止swagger,prod可以设置为false + path: /swagger-ui.html #swagger页面 + csrf: + enabled: true # 启用CSRF支持 + api-docs: + enabled: true #开启/禁止api-docs, prod可以设置为false + path: /api-docs #api的json文档 + use-management-port: false + enable-spring-security: true # 防止XSS攻击 xss: diff --git a/ruoyi-common/ruoyi-common-springdoc/pom.xml b/ruoyi-common/ruoyi-common-springdoc/pom.xml index 671349e..54a8bf8 100644 --- a/ruoyi-common/ruoyi-common-springdoc/pom.xml +++ b/ruoyi-common/ruoyi-common-springdoc/pom.xml @@ -16,7 +16,27 @@ + + + org.springdoc + springdoc-openapi-starter-webmvc-ui + + + org.springdoc + springdoc-openapi-starter-webmvc-api + + + + org.springdoc + springdoc-openapi-starter-common + + + + + com.github.therapi + therapi-runtime-javadoc + diff --git a/ruoyi-common/ruoyi-common-springdoc/src/main/java/com/ruoyi/common/springdoc/SpringDocConfig.java b/ruoyi-common/ruoyi-common-springdoc/src/main/java/com/ruoyi/common/springdoc/SpringDocConfig.java new file mode 100644 index 0000000..3a1daec --- /dev/null +++ b/ruoyi-common/ruoyi-common-springdoc/src/main/java/com/ruoyi/common/springdoc/SpringDocConfig.java @@ -0,0 +1,45 @@ +package com.ruoyi.common.springdoc; + +import io.swagger.v3.oas.models.Components; +import io.swagger.v3.oas.models.ExternalDocumentation; +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.SecurityRequirement; +import io.swagger.v3.oas.models.security.SecurityScheme; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.ArrayList; +import java.util.List; + +/** + * SpringDoc API文档相关配置 + * @author 数据小王子 on 2023/07/24. + */ +@Configuration +public class SpringDocConfig { + @Bean + public OpenAPI openAPI() { + SecurityRequirement securityRequirement = new SecurityRequirement().addList("Authorization"); // 名字和创建的SecuritySchemes一致 + List list = new ArrayList<>(); + list.add(securityRequirement); + return new OpenAPI() + .components(new Components().addSecuritySchemes("Authorization", // key值 + new SecurityScheme() + .type(SecurityScheme.Type.APIKEY) //请求认证类型 + .scheme("bearer").bearerFormat("JWT") + .name("Authorization") //API key参数名 + .description("token令牌") //API key描述 + .in(SecurityScheme.In.HEADER))) //设置API key的存放位置 + .security(list) + .info(new Info().title("Ruoyi-Flex API Doc") + .description("Ruoyi-Flex SrpingDoc demo") + .version("v4.1.2") + .license(new License().name("MIT").url("https://opensource.org/licenses/MIT"))) + .externalDocs(new ExternalDocumentation() + .description("Ruoyi-Flex Wiki Documentation") + .url("https://gitee.com/dataprince/ruoyi-flex")); + } + +} diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java index 208b03d..1bee220 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java @@ -111,7 +111,8 @@ public class SecurityConfig .requestMatchers("/login", "/register", "/captchaImage","/captcha/get", "/captcha/check").permitAll() // 静态资源,可匿名访问 .requestMatchers(HttpMethod.GET, "/*.html", "/*/*.html", "/*/*.css", "/*/*.js", "/profile/**").permitAll() - .requestMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs").permitAll() + .requestMatchers("/swagger-resources/**", "/webjars/**", "/*/api-docs").permitAll() + .requestMatchers("/swagger-ui.html","/swagger-ui/**", "/v3/api-docs/**").permitAll() //设置 @PermitAll 无需认证 .requestMatchers(HttpMethod.GET, permitAllUrls.get(HttpMethod.GET).toArray(new String[0])).permitAll() .requestMatchers(HttpMethod.POST, permitAllUrls.get(HttpMethod.POST).toArray(new String[0])).permitAll() diff --git a/ruoyi-ui/src/views/tool/swagger/index.vue b/ruoyi-ui/src/views/tool/swagger/index.vue index b8becc6..7b10e8c 100644 --- a/ruoyi-ui/src/views/tool/swagger/index.vue +++ b/ruoyi-ui/src/views/tool/swagger/index.vue @@ -8,7 +8,7 @@ export default { components: { iFrame }, data() { return { - url: process.env.VUE_APP_BASE_API + "/swagger-ui/index.html" + url: process.env.VUE_APP_BASE_API + "/swagger-ui.html" }; }, };