20
20
import springfox .documentation .spi .DocumentationType ;
21
21
import springfox .documentation .spi .service .contexts .SecurityContext ;
22
22
import springfox .documentation .spring .web .plugins .Docket ;
23
+ import springfox .documentation .swagger .web .ApiKeyVehicle ;
23
24
import springfox .documentation .swagger .web .UiConfiguration ;
25
+ import springfox .documentation .swagger .web .UiConfigurationBuilder ;
24
26
25
27
import java .util .*;
26
28
import java .util .stream .Collectors ;
@@ -48,15 +50,20 @@ public SwaggerProperties swaggerProperties() {
48
50
49
51
@ Bean
50
52
public UiConfiguration uiConfiguration (SwaggerProperties swaggerProperties ) {
51
- return new UiConfiguration (
52
- swaggerProperties .getUiConfig ().getValidatorUrl (),// url
53
- swaggerProperties .getUiConfig ().getDocExpansion (), // docExpansion => none | list
54
- swaggerProperties .getUiConfig ().getApiSorter (), // apiSorter => alpha
55
- swaggerProperties .getUiConfig ().getDefaultModelRendering (), // defaultModelRendering => schema
56
- swaggerProperties .getUiConfig ().getSubmitMethods ().split ("," ),
57
- swaggerProperties .getUiConfig ().getJsonEditor (), // enableJsonEditor => true | false
58
- swaggerProperties .getUiConfig ().getShowRequestHeaders (), // showRequestHeaders => true | false
59
- swaggerProperties .getUiConfig ().getRequestTimeout ()); // requestTimeout => in milliseconds, defaults to null (uses jquery xh timeout)
53
+ return UiConfigurationBuilder .builder ()
54
+ .deepLinking (swaggerProperties .getUiConfig ().getDeepLinking ())
55
+ .defaultModelExpandDepth (swaggerProperties .getUiConfig ().getDefaultModelExpandDepth ())
56
+ .defaultModelRendering (swaggerProperties .getUiConfig ().getDefaultModelRendering ())
57
+ .defaultModelsExpandDepth (swaggerProperties .getUiConfig ().getDefaultModelsExpandDepth ())
58
+ .displayOperationId (swaggerProperties .getUiConfig ().getDisplayOperationId ())
59
+ .displayRequestDuration (swaggerProperties .getUiConfig ().getDisplayRequestDuration ())
60
+ .docExpansion (swaggerProperties .getUiConfig ().getDocExpansion ())
61
+ .maxDisplayedTags (swaggerProperties .getUiConfig ().getMaxDisplayedTags ())
62
+ .operationsSorter (swaggerProperties .getUiConfig ().getOperationsSorter ())
63
+ .showExtensions (swaggerProperties .getUiConfig ().getShowExtensions ())
64
+ .tagsSorter (swaggerProperties .getUiConfig ().getTagsSorter ())
65
+ .validatorUrl (swaggerProperties .getUiConfig ().getValidatorUrl ())
66
+ .build ();
60
67
}
61
68
62
69
@ Bean
@@ -92,16 +99,16 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
92
99
}
93
100
94
101
// exclude-path处理
95
- List <Predicate <String >> excludePath = new ArrayList ();
102
+ List <Predicate <String >> excludePath = new ArrayList <> ();
96
103
for (String path : swaggerProperties .getExcludePath ()) {
97
104
excludePath .add (PathSelectors .ant (path ));
98
105
}
99
106
100
107
Docket docketForBuilder = new Docket (DocumentationType .SWAGGER_2 )
101
108
.host (swaggerProperties .getHost ())
102
109
.apiInfo (apiInfo )
103
- .securitySchemes (this . securitySchemes ( ))
104
- .securityContexts (this . securityContexts ( ))
110
+ .securitySchemes (Collections . singletonList ( apiKey () ))
111
+ .securityContexts (Collections . singletonList ( securityContext () ))
105
112
.globalOperationParameters (buildGlobalOperationParametersFromSwaggerProperties (
106
113
swaggerProperties .getGlobalOperationParameters ()));
107
114
@@ -119,9 +126,9 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
119
126
)
120
127
).build ();
121
128
122
- /** ignoredParameterTypes **/
123
- Class [] array = new Class [swaggerProperties .getIgnoredParameterTypes ().size ()];
124
- Class [] ignoredParameterTypes = swaggerProperties .getIgnoredParameterTypes ().toArray (array );
129
+ /* ignoredParameterTypes **/
130
+ Class <?> [] array = new Class [swaggerProperties .getIgnoredParameterTypes ().size ()];
131
+ Class <?> [] ignoredParameterTypes = swaggerProperties .getIgnoredParameterTypes ().toArray (array );
125
132
docket .ignoredParameterTypes (ignoredParameterTypes );
126
133
127
134
configurableBeanFactory .registerSingleton ("defaultDocket" , docket );
@@ -168,8 +175,8 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
168
175
Docket docketForBuilder = new Docket (DocumentationType .SWAGGER_2 )
169
176
.host (swaggerProperties .getHost ())
170
177
.apiInfo (apiInfo )
171
- .securitySchemes (this . securitySchemes ( ))
172
- .securityContexts (this . securityContexts ( ))
178
+ .securitySchemes (Collections . singletonList ( apiKey () ))
179
+ .securityContexts (Collections . singletonList ( securityContext () ))
173
180
.globalOperationParameters (assemblyGlobalOperationParameters (swaggerProperties .getGlobalOperationParameters (),
174
181
docketInfo .getGlobalOperationParameters ()));
175
182
@@ -189,9 +196,9 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
189
196
)
190
197
.build ();
191
198
192
- /** ignoredParameterTypes **/
193
- Class [] array = new Class [docketInfo .getIgnoredParameterTypes ().size ()];
194
- Class [] ignoredParameterTypes = docketInfo .getIgnoredParameterTypes ().toArray (array );
199
+ /* ignoredParameterTypes **/
200
+ Class <?> [] array = new Class [docketInfo .getIgnoredParameterTypes ().size ()];
201
+ Class <?> [] ignoredParameterTypes = docketInfo .getIgnoredParameterTypes ().toArray (array );
195
202
docket .ignoredParameterTypes (ignoredParameterTypes );
196
203
197
204
configurableBeanFactory .registerSingleton (groupName , docket );
@@ -201,36 +208,41 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
201
208
}
202
209
203
210
/**
204
- * 配置 Authorization ApiKey
211
+ * 配置基于 ApiKey 的鉴权对象
205
212
*
206
213
* @return
207
214
*/
208
- private List < ApiKey > securitySchemes () {
209
- return newArrayList (
210
- new ApiKey ( swaggerProperties ().getAuthorization ().getName (),
211
- swaggerProperties (). getAuthorization (). getKeyName (), "header" ));
215
+ private ApiKey apiKey () {
216
+ return new ApiKey ( swaggerProperties (). getAuthorization (). getName (),
217
+ swaggerProperties ().getAuthorization ().getKeyName (),
218
+ ApiKeyVehicle . HEADER . getValue ( ));
212
219
}
213
220
214
221
/**
215
- * 通过正则设置需要传递 Authorization 信息的API接口
222
+ * 配置默认的全局鉴权策略的开关,以及通过正则表达式进行匹配;默认 ^.*$ 匹配所有URL
223
+ * 其中 securityReferences 为配置启用的鉴权策略
216
224
*
217
225
* @return
218
226
*/
219
- private List <SecurityContext > securityContexts () {
220
- return newArrayList (
221
- SecurityContext .builder ()
222
- .securityReferences (defaultAuth ())
223
- .forPaths (PathSelectors .regex (swaggerProperties ().getAuthorization ().getAuthRegex ()))
224
- .build ()
225
- );
227
+ private SecurityContext securityContext () {
228
+ return SecurityContext .builder ()
229
+ .securityReferences (defaultAuth ())
230
+ .forPaths (PathSelectors .regex (swaggerProperties ().getAuthorization ().getAuthRegex ()))
231
+ .build ();
226
232
}
227
233
228
- List <SecurityReference > defaultAuth () {
234
+ /**
235
+ * 配置默认的全局鉴权策略;其中返回的 SecurityReference 中,reference 即为ApiKey对象里面的name,保持一致才能开启全局鉴权
236
+ *
237
+ * @return
238
+ */
239
+ private List <SecurityReference > defaultAuth () {
229
240
AuthorizationScope authorizationScope = new AuthorizationScope ("global" , "accessEverything" );
230
241
AuthorizationScope [] authorizationScopes = new AuthorizationScope [1 ];
231
242
authorizationScopes [0 ] = authorizationScope ;
232
- return newArrayList (
233
- new SecurityReference ("BearerToken" , authorizationScopes ));
243
+ return Collections .singletonList (SecurityReference .builder ()
244
+ .reference (swaggerProperties ().getAuthorization ().getName ())
245
+ .scopes (authorizationScopes ).build ());
234
246
}
235
247
236
248
@@ -295,15 +307,15 @@ private List<Parameter> assemblyGlobalOperationParameters(
295
307
/**
296
308
* 设置全局响应消息
297
309
*
298
- * @param swaggerProperties 支持 POST,GET,PUT,PATCH,DELETE,HEAD,OPTIONS,TRACE
299
- * @param docketForBuilder
310
+ * @param swaggerProperties swaggerProperties 支持 POST,GET,PUT,PATCH,DELETE,HEAD,OPTIONS,TRACE
311
+ * @param docketForBuilder swagger docket builder
300
312
*/
301
313
private void buildGlobalResponseMessage (SwaggerProperties swaggerProperties , Docket docketForBuilder ) {
302
314
303
315
SwaggerProperties .GlobalResponseMessage globalResponseMessages =
304
316
swaggerProperties .getGlobalResponseMessage ();
305
317
306
- // POST,GET,PUT,PATCH,DELETE,HEAD,OPTIONS,TRACE 响应消息体
318
+ /* POST,GET,PUT,PATCH,DELETE,HEAD,OPTIONS,TRACE 响应消息体 **/
307
319
List <ResponseMessage > postResponseMessages = getResponseMessageList (globalResponseMessages .getPost ());
308
320
List <ResponseMessage > getResponseMessages = getResponseMessageList (globalResponseMessages .getGet ());
309
321
List <ResponseMessage > putResponseMessages = getResponseMessageList (globalResponseMessages .getPut ());
@@ -327,10 +339,11 @@ private void buildGlobalResponseMessage(SwaggerProperties swaggerProperties, Doc
327
339
/**
328
340
* 获取返回消息体列表
329
341
*
330
- * @param globalResponseMessageBodyList
342
+ * @param globalResponseMessageBodyList 全局Code消息返回集合
331
343
* @return
332
344
*/
333
- private List <ResponseMessage > getResponseMessageList (List <SwaggerProperties .GlobalResponseMessageBody > globalResponseMessageBodyList ) {
345
+ private List <ResponseMessage > getResponseMessageList
346
+ (List <SwaggerProperties .GlobalResponseMessageBody > globalResponseMessageBodyList ) {
334
347
List <ResponseMessage > responseMessages = new ArrayList <>();
335
348
for (SwaggerProperties .GlobalResponseMessageBody globalResponseMessageBody : globalResponseMessageBodyList ) {
336
349
ResponseMessageBuilder responseMessageBuilder = new ResponseMessageBuilder ();
0 commit comments