SwaggerConfiguration.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * Neither the name of the dreamlu.net developer nor the names of its
  13. * contributors may be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. * Author: Chill 庄骞 (smallchill@163.com)
  16. */
  17. package org.springblade.common.config;
  18. import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
  19. import com.google.common.collect.Lists;
  20. import lombok.AllArgsConstructor;
  21. import org.springblade.core.launch.constant.AppConstant;
  22. import org.springblade.core.swagger.EnableSwagger;
  23. import org.springblade.core.swagger.SwaggerProperties;
  24. import org.springblade.core.swagger.SwaggerUtil;
  25. import org.springframework.context.annotation.Bean;
  26. import org.springframework.context.annotation.Configuration;
  27. import springfox.documentation.builders.ApiInfoBuilder;
  28. import springfox.documentation.builders.PathSelectors;
  29. import springfox.documentation.service.ApiInfo;
  30. import springfox.documentation.service.Contact;
  31. import springfox.documentation.service.SecurityScheme;
  32. import springfox.documentation.spi.DocumentationType;
  33. import springfox.documentation.spring.web.plugins.Docket;
  34. import java.util.Arrays;
  35. import java.util.Collections;
  36. import java.util.List;
  37. /**
  38. * Swagger配置类
  39. *
  40. * @author Chill
  41. */
  42. @Configuration
  43. @EnableSwagger
  44. @AllArgsConstructor
  45. public class SwaggerConfiguration {
  46. /**
  47. * 引入swagger配置类
  48. */
  49. private final SwaggerProperties swaggerProperties;
  50. /**
  51. * 引入Knife4j扩展类
  52. */
  53. private final OpenApiExtensionResolver openApiExtensionResolver;
  54. @Bean
  55. public Docket authDocket() {
  56. return docket("授权模块", Collections.singletonList(AppConstant.BASE_PACKAGES + ".modules.auth"));
  57. }
  58. @Bean
  59. public Docket sysDocket() {
  60. return docket("系统模块",
  61. Arrays.asList(AppConstant.BASE_PACKAGES + ".modules.system", AppConstant.BASE_PACKAGES + ".modules.resource"));
  62. }
  63. @Bean
  64. public Docket flowDocket() {
  65. return docket("工作流模块", Collections.singletonList(AppConstant.BASE_PACKAGES + ".flow"));
  66. }
  67. private Docket docket(String groupName, List<String> basePackages) {
  68. return new Docket(DocumentationType.SWAGGER_2)
  69. .groupName(groupName)
  70. .apiInfo(apiInfo())
  71. .select()
  72. .apis(SwaggerUtil.basePackages(basePackages))
  73. .paths(PathSelectors.any())
  74. .build().securitySchemes(Lists.<SecurityScheme>newArrayList(SwaggerUtil.clientInfo(), SwaggerUtil.bladeAuth(), SwaggerUtil.bladeTenant()))
  75. .extensions(openApiExtensionResolver.buildExtensions(groupName));
  76. }
  77. private ApiInfo apiInfo() {
  78. return new ApiInfoBuilder()
  79. .title(swaggerProperties.getTitle())
  80. .description(swaggerProperties.getDescription())
  81. .license(swaggerProperties.getLicense())
  82. .licenseUrl(swaggerProperties.getLicenseUrl())
  83. .termsOfServiceUrl(swaggerProperties.getTermsOfServiceUrl())
  84. .contact(new Contact(swaggerProperties.getContact().getName(), swaggerProperties.getContact().getUrl(), swaggerProperties.getContact().getEmail()))
  85. .version(swaggerProperties.getVersion())
  86. .build();
  87. }
  88. }