From 22034662f3568e59f9f832e7bdfd9bbbfcd031ed Mon Sep 17 00:00:00 2001 From: wanggeng <450292408@qq.com> Date: Thu, 12 May 2022 16:05:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BE=9D=E8=B5=96=EF=BC=8C?= =?UTF-8?q?=E6=95=B4=E7=90=86swagger2=203.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- basic-pojo/pom.xml | 10 +- .../properties/swagger/SwaggerProperties.java | 62 ++++++ .../wgink/common/config/SwaggerConfig.java | 192 +++++------------- login-oauth2-client/pom.xml | 2 +- login-oauth2-server/pom.xml | 2 +- .../app/api/FileV2AppController.java | 4 +- pom.xml | 4 +- 7 files changed, 127 insertions(+), 149 deletions(-) create mode 100644 basic-properties/src/main/java/ink/wgink/properties/swagger/SwaggerProperties.java diff --git a/basic-pojo/pom.xml b/basic-pojo/pom.xml index 967ea9fd..6dc93f93 100644 --- a/basic-pojo/pom.xml +++ b/basic-pojo/pom.xml @@ -54,15 +54,11 @@ + io.springfox - springfox-swagger2 - - - org.slf4j - slf4j-api - - + springfox-boot-starter + 3.0.0 io.springfox diff --git a/basic-properties/src/main/java/ink/wgink/properties/swagger/SwaggerProperties.java b/basic-properties/src/main/java/ink/wgink/properties/swagger/SwaggerProperties.java new file mode 100644 index 00000000..4d8eae3f --- /dev/null +++ b/basic-properties/src/main/java/ink/wgink/properties/swagger/SwaggerProperties.java @@ -0,0 +1,62 @@ +package ink.wgink.properties.swagger; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +/** + * @ClassName: SwaggerProperties + * @Description: swagger配置 + * @Author: wanggeng + * @Date: 2022/5/12 14:21 + * @Version: 1.0 + */ +@Component +@ConfigurationProperties(prefix = "swagger") +public class SwaggerProperties { + + private Boolean active; + private String title; + private String description; + private String host; + private String version; + + public Boolean getActive() { + return active == null ? true : active; + } + + public void setActive(Boolean active) { + this.active = active; + } + + public String getTitle() { + return title == null ? "相关接口文档" : title.trim(); + } + + public void setTitle(String title) { + this.title = title; + } + + public String getDescription() { + return description == null ? "相关接口文档" : description.trim(); + } + + public void setDescription(String description) { + this.description = description; + } + + public String getHost() { + return host == null ? "http://www.wgink.ink" : host.trim(); + } + + public void setHost(String host) { + this.host = host; + } + + public String getVersion() { + return version == null ? "" : version.trim(); + } + + public void setVersion(String version) { + this.version = version; + } +} diff --git a/common/src/main/java/ink/wgink/common/config/SwaggerConfig.java b/common/src/main/java/ink/wgink/common/config/SwaggerConfig.java index b8b4f9eb..018a0c19 100644 --- a/common/src/main/java/ink/wgink/common/config/SwaggerConfig.java +++ b/common/src/main/java/ink/wgink/common/config/SwaggerConfig.java @@ -1,26 +1,20 @@ package ink.wgink.common.config; -import com.google.common.base.Function; -import com.google.common.base.Optional; -import com.google.common.base.Predicate; -import com.google.common.base.Predicates; import ink.wgink.interfaces.consts.ISystemConstant; -import org.springframework.beans.factory.annotation.Value; +import ink.wgink.properties.swagger.SwaggerProperties; +import io.swagger.models.auth.In; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.web.bind.annotation.RequestMethod; -import springfox.documentation.RequestHandler; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; -import springfox.documentation.builders.ResponseMessageBuilder; -import springfox.documentation.service.ApiInfo; -import springfox.documentation.service.ResponseMessage; +import springfox.documentation.service.*; import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spi.service.contexts.SecurityContext; import springfox.documentation.spring.web.plugins.Docket; -import javax.annotation.PostConstruct; -import java.util.ArrayList; +import java.util.Collections; import java.util.List; /** @@ -32,174 +26,100 @@ import java.util.List; **/ @Configuration public class SwaggerConfig { - - @Value("${swagger.title:相关接口文档}") - private String title; - @Value("${swagger.description:相关接口文档}") - private String description; - @Value("${swagger.service-url:http://www.wgink.ink}") - private String serviceUrl; - @Value("${swagger.version:1.0}") - private String version; - @Value("#{'${swagger.base-package-list}'.split(',')}") - private List basePackageList; - private Predicate[] basePackages; - - @PostConstruct - private void initBasePackages() { - basePackages = new Predicate[basePackageList.size()]; - for (int i = 0; i < basePackages.length; i++) { - basePackages[i] = RequestHandlerSelectors.basePackage(basePackageList.get(i)); - } - } - - private List responseMessageList() { - List responseMessageList = new ArrayList<>(); - responseMessageList.add(new ResponseMessageBuilder().code(400).message("请求失败").build()); - responseMessageList.add(new ResponseMessageBuilder().code(404).message("请求不存在").build()); - responseMessageList.add(new ResponseMessageBuilder().code(503).message("服务不可用").build()); - return responseMessageList; - } - - @Bean - public Docket createRestGlobalApi() { - List responseMessageList = responseMessageList(); - return new Docket(DocumentationType.SWAGGER_2) - .globalResponseMessage(RequestMethod.POST, responseMessageList) - .globalResponseMessage(RequestMethod.DELETE, responseMessageList) - .globalResponseMessage(RequestMethod.PUT, responseMessageList) - .globalResponseMessage(RequestMethod.GET, responseMessageList) - .apiInfo(apiInfo()) - .select() - .apis(Predicates.or(basePackages)) - .paths(PathSelectors.any()) - .build(); - } + @Autowired + private SwaggerProperties swaggerProperties; @Bean public Docket createRestApiSystem() { - List responseMessageList = responseMessageList(); - return new Docket(DocumentationType.SWAGGER_2) - .globalResponseMessage(RequestMethod.POST, responseMessageList) - .globalResponseMessage(RequestMethod.DELETE, responseMessageList) - .globalResponseMessage(RequestMethod.PUT, responseMessageList) - .globalResponseMessage(RequestMethod.GET, responseMessageList) + return new Docket(DocumentationType.OAS_30) + .enable(swaggerProperties.getActive()) + .pathMapping("/") .groupName(ISystemConstant.API_GROUP_SYSTEM) .apiInfo(apiInfo()) .select() - .apis(Predicates.or(basePackages)) - .paths(PathSelectors.ant(ISystemConstant.API_PREFIX + "/**")) + .apis(RequestHandlerSelectors.any()) + .paths(PathSelectors.ant("/*" + ISystemConstant.API_PREFIX + "/**")) .build(); } @Bean public Docket createRestApiResource() { - List responseMessageList = responseMessageList(); - return new Docket(DocumentationType.SWAGGER_2) - .globalResponseMessage(RequestMethod.POST, responseMessageList) - .globalResponseMessage(RequestMethod.DELETE, responseMessageList) - .globalResponseMessage(RequestMethod.PUT, responseMessageList) - .globalResponseMessage(RequestMethod.GET, responseMessageList) + return new Docket(DocumentationType.OAS_30) + .enable(swaggerProperties.getActive()) + .pathMapping("/") .groupName(ISystemConstant.API_GROUP_RESOURCE) .apiInfo(apiInfo()) .select() - .apis(Predicates.or(basePackages)) - .paths(PathSelectors.ant(ISystemConstant.RESOURCE_PREFIX + "/**")) + .apis(RequestHandlerSelectors.any()) + .paths(PathSelectors.ant("/*" + ISystemConstant.RESOURCE_PREFIX + "/**")) .build(); } @Bean public Docket createRestApiApp() { - List responseMessageList = responseMessageList(); - return new Docket(DocumentationType.SWAGGER_2) - .globalResponseMessage(RequestMethod.POST, responseMessageList) - .globalResponseMessage(RequestMethod.DELETE, responseMessageList) - .globalResponseMessage(RequestMethod.PUT, responseMessageList) - .globalResponseMessage(RequestMethod.GET, responseMessageList) + return new Docket(DocumentationType.OAS_30) + .enable(swaggerProperties.getActive()) + .pathMapping("/") .groupName(ISystemConstant.API_GROUP_APP) .apiInfo(apiInfo()) .select() - .apis(Predicates.or(basePackages)) - .paths(PathSelectors.ant(ISystemConstant.APP_PREFIX + "/**")) - .build(); - } - - @Bean - public Docket createRestApiRoute() { - List responseMessageList = responseMessageList(); - return new Docket(DocumentationType.SWAGGER_2) - .globalResponseMessage(RequestMethod.POST, responseMessageList) - .globalResponseMessage(RequestMethod.DELETE, responseMessageList) - .globalResponseMessage(RequestMethod.PUT, responseMessageList) - .globalResponseMessage(RequestMethod.GET, responseMessageList) - .groupName(ISystemConstant.API_GROUP_ROUTE) - .apiInfo(apiInfo()) - .select() - .apis(Predicates.or(basePackages)) - .paths(PathSelectors.ant("/route/**")) + .apis(RequestHandlerSelectors.any()) + .paths(PathSelectors.ant("/*" + ISystemConstant.APP_PREFIX + "/**")) .build(); } @Bean public Docket createRestApiWechat() { - List responseMessageList = responseMessageList(); - return new Docket(DocumentationType.SWAGGER_2) - .globalResponseMessage(RequestMethod.POST, responseMessageList) - .globalResponseMessage(RequestMethod.DELETE, responseMessageList) - .globalResponseMessage(RequestMethod.PUT, responseMessageList) - .globalResponseMessage(RequestMethod.GET, responseMessageList) + return new Docket(DocumentationType.OAS_30) + .enable(swaggerProperties.getActive()) + .pathMapping("/") .groupName(ISystemConstant.API_GROUP_WECHAT) .apiInfo(apiInfo()) .select() - .apis(Predicates.or(basePackages)) - .paths(PathSelectors.ant(ISystemConstant.WECHAT_PREFIX + "/**")) + .apis(RequestHandlerSelectors.any()) + .paths(PathSelectors.ant("/*" + ISystemConstant.WECHAT_PREFIX + "/**")) .build(); } @Bean public Docket createRestApiWechatMiniapp() { - List responseMessageList = responseMessageList(); - return new Docket(DocumentationType.SWAGGER_2) - .globalResponseMessage(RequestMethod.POST, responseMessageList) - .globalResponseMessage(RequestMethod.DELETE, responseMessageList) - .globalResponseMessage(RequestMethod.PUT, responseMessageList) - .globalResponseMessage(RequestMethod.GET, responseMessageList) + return new Docket(DocumentationType.OAS_30) + .enable(swaggerProperties.getActive()) + .pathMapping("/") .groupName(ISystemConstant.API_GROUP_WECHAT_MINI_APP) .apiInfo(apiInfo()) .select() - .apis(Predicates.or(basePackages)) - .paths(PathSelectors.ant(ISystemConstant.WECHAT_MINI_APP_PREFIX + "/**")) + .apis(RequestHandlerSelectors.any()) + .paths(PathSelectors.ant("/*" + ISystemConstant.WECHAT_MINI_APP_PREFIX + "/**")) .build(); } + /** + * 设置授权信息 + */ + private List securitySchemes() { + ApiKey apiKey = new ApiKey("BASE_TOKEN", "token", In.HEADER.toValue()); + return Collections.singletonList(apiKey); + } + + /** + * 授权信息全局应用 + */ + private List securityContexts() { + return Collections.singletonList( + SecurityContext.builder() + .securityReferences(Collections.singletonList(new SecurityReference("BASE_TOKEN", new AuthorizationScope[]{new AuthorizationScope("global", "")}))) + .build() + ); + } + private ApiInfo apiInfo() { return new ApiInfoBuilder() - .title(title) - .description(description) - .termsOfServiceUrl(serviceUrl) - .version(version) + .title(swaggerProperties.getTitle()) + .description(swaggerProperties.getDescription()) + .contact(new Contact("WenG", null, "450292408@qq.com")) + .version(swaggerProperties.getVersion()) .build(); } - public static Predicate basePackage(final List basePackageList) { - return input -> declaringClass(input).transform(handlerPackage(basePackageList)).or(true); - } - - private static Function, Boolean> handlerPackage(final List basePackageList) { - return input -> { - // 循环判断匹配 - for (String strPackage : basePackageList) { - boolean isMatch = input.getPackage().getName().startsWith(strPackage); - if (isMatch) { - return true; - } - } - return false; - }; - } - - private static Optional> declaringClass(RequestHandler input) { - return Optional.fromNullable(input.getClass()); - } - } diff --git a/login-oauth2-client/pom.xml b/login-oauth2-client/pom.xml index d12bfc74..2790086b 100644 --- a/login-oauth2-client/pom.xml +++ b/login-oauth2-client/pom.xml @@ -36,7 +36,7 @@ org.springframework.security.oauth.boot spring-security-oauth2-autoconfigure - 2.0.0.RELEASE + 2.5.13 com.fasterxml.jackson.core diff --git a/login-oauth2-server/pom.xml b/login-oauth2-server/pom.xml index 7e45b495..92dc70b1 100644 --- a/login-oauth2-server/pom.xml +++ b/login-oauth2-server/pom.xml @@ -46,7 +46,7 @@ org.springframework.security.oauth.boot spring-security-oauth2-autoconfigure - 2.0.0.RELEASE + 2.5.13 com.fasterxml.jackson.core diff --git a/module-file/src/main/java/ink/wgink/module/file/controller/app/api/FileV2AppController.java b/module-file/src/main/java/ink/wgink/module/file/controller/app/api/FileV2AppController.java index 1feb0fa5..5afce069 100644 --- a/module-file/src/main/java/ink/wgink/module/file/controller/app/api/FileV2AppController.java +++ b/module-file/src/main/java/ink/wgink/module/file/controller/app/api/FileV2AppController.java @@ -46,7 +46,7 @@ public class FileV2AppController extends DefaultBaseController { @ApiOperation(value = "上传文件", notes = "上传文件接口") @ApiImplicitParams({ - @ApiImplicitParam(name = "userId", value = "userId", paramType = "token"), + @ApiImplicitParam(name = "userId", value = "userId", paramType = "header"), @ApiImplicitParam(name = "file", value = "文件name", paramType = "query") }) @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) @@ -70,7 +70,7 @@ public class FileV2AppController extends DefaultBaseController { @ApiOperation(value = "上传图片", notes = "上传图片接口") @ApiImplicitParams({ - @ApiImplicitParam(name = "userId", value = "userId", paramType = "token"), + @ApiImplicitParam(name = "userId", value = "userId", paramType = "header"), @ApiImplicitParam(name = "image", value = "文件name", paramType = "query") }) @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) diff --git a/pom.xml b/pom.xml index 5a1f28af..5d6d98e7 100644 --- a/pom.xml +++ b/pom.xml @@ -69,8 +69,8 @@ 2.0 1.2 0.4.8 - 2.5.0 - 2.0.9 + 3.0.0 + 3.0.3 2.9.4 1.6 2.6.0