wujian
2024-04-17 8c05d8c9715a26e1b7a01db431d1262730d8c408
提交 | 用户 | 时间
e57a89 1 package com.jcdm.web.core.config;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import org.springframework.beans.factory.annotation.Autowired;
6 import org.springframework.beans.factory.annotation.Value;
7 import org.springframework.context.annotation.Bean;
8 import org.springframework.context.annotation.Configuration;
9 import com.jcdm.common.config.MesConfig;
10 import io.swagger.annotations.ApiOperation;
11 import io.swagger.models.auth.In;
12 import springfox.documentation.builders.ApiInfoBuilder;
13 import springfox.documentation.builders.PathSelectors;
14 import springfox.documentation.builders.RequestHandlerSelectors;
15 import springfox.documentation.service.ApiInfo;
16 import springfox.documentation.service.ApiKey;
17 import springfox.documentation.service.AuthorizationScope;
18 import springfox.documentation.service.Contact;
19 import springfox.documentation.service.SecurityReference;
20 import springfox.documentation.service.SecurityScheme;
21 import springfox.documentation.spi.DocumentationType;
22 import springfox.documentation.spi.service.contexts.SecurityContext;
23 import springfox.documentation.spring.web.plugins.Docket;
24
25 /**
26  * Swagger2的接口配置
27  * 
28  * @author jc
29  */
30 @Configuration
31 public class SwaggerConfig
32 {
33     /** 系统基础配置 */
34     @Autowired
35     private MesConfig mesConfig;
36
37     /** 是否开启swagger */
38     @Value("${swagger.enabled}")
39     private boolean enabled;
40
41     /** 设置请求的统一前缀 */
42     @Value("${swagger.pathMapping}")
43     private String pathMapping;
44
45     /**
46      * 创建API
47      */
48     @Bean
49     public Docket createRestApi()
50     {
51         return new Docket(DocumentationType.OAS_30)
52                 // 是否启用Swagger
53                 .enable(enabled)
54                 // 用来创建该API的基本信息,展示在文档的页面中(自定义展示的信息)
55                 .apiInfo(apiInfo())
56                 // 设置哪些接口暴露给Swagger展示
57                 .select()
58                 // 扫描所有有注解的api,用这种方式更灵活
59                 .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
60                 // 扫描指定包中的swagger注解
61                 // .apis(RequestHandlerSelectors.basePackage("com.jcdm.project.tool.swagger"))
62                 // 扫描所有 .apis(RequestHandlerSelectors.any())
63                 .paths(PathSelectors.any())
64                 .build()
65                 /* 设置安全模式,swagger可以设置访问token */
66                 .securitySchemes(securitySchemes())
67                 .securityContexts(securityContexts())
68                 .pathMapping(pathMapping);
69     }
70
71     /**
72      * 安全模式,这里指定token通过Authorization头请求头传递
73      */
74     private List<SecurityScheme> securitySchemes()
75     {
76         List<SecurityScheme> apiKeyList = new ArrayList<SecurityScheme>();
77         apiKeyList.add(new ApiKey("Authorization", "Authorization", In.HEADER.toValue()));
78         return apiKeyList;
79     }
80
81     /**
82      * 安全上下文
83      */
84     private List<SecurityContext> securityContexts()
85     {
86         List<SecurityContext> securityContexts = new ArrayList<>();
87         securityContexts.add(
88                 SecurityContext.builder()
89                         .securityReferences(defaultAuth())
90                         .operationSelector(o -> o.requestMappingPattern().matches("/.*"))
91                         .build());
92         return securityContexts;
93     }
94
95     /**
96      * 默认的安全上引用
97      */
98     private List<SecurityReference> defaultAuth()
99     {
100         AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
101         AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
102         authorizationScopes[0] = authorizationScope;
103         List<SecurityReference> securityReferences = new ArrayList<>();
104         securityReferences.add(new SecurityReference("Authorization", authorizationScopes));
105         return securityReferences;
106     }
107
108     /**
109      * 添加摘要信息
110      */
111     private ApiInfo apiInfo()
112     {
113         // 用ApiInfoBuilder进行定制
114         return new ApiInfoBuilder()
115                 // 设置标题
116                 .title("标题:江宸MES管理系统_接口文档")
117                 // 描述
118                 .description("描述:用于工厂生产现场管理")
119                 // 作者信息
120                 .contact(new Contact(mesConfig.getName(), null, null))
121                 // 版本
122                 .version("版本号:" + mesConfig.getVersion())
123                 .build();
124     }
125 }