2
2
3
3
import com .google .common .base .Predicate ;
4
4
import com .google .common .base .Predicates ;
5
- import com .google .common .collect .Lists ;
6
5
import org .springframework .beans .BeansException ;
7
6
import org .springframework .beans .factory .BeanFactory ;
8
7
import org .springframework .beans .factory .BeanFactoryAware ;
17
16
import org .springframework .web .bind .annotation .RequestMethod ;
18
17
import springfox .documentation .builders .*;
19
18
import springfox .documentation .schema .ModelRef ;
20
- import springfox .documentation .service .ApiInfo ;
21
- import springfox .documentation .service .Contact ;
22
- import springfox .documentation .service .Parameter ;
23
- import springfox .documentation .service .ResponseMessage ;
19
+ import springfox .documentation .service .*;
24
20
import springfox .documentation .spi .DocumentationType ;
21
+ import springfox .documentation .spi .service .contexts .SecurityContext ;
25
22
import springfox .documentation .spring .web .plugins .Docket ;
23
+ import springfox .documentation .swagger .web .ApiKeyVehicle ;
26
24
import springfox .documentation .swagger .web .UiConfiguration ;
25
+ import springfox .documentation .swagger .web .UiConfigurationBuilder ;
27
26
28
27
import java .util .*;
29
28
import java .util .stream .Collectors ;
30
29
30
+ import static com .google .common .collect .Lists .newArrayList ;
31
+
31
32
/**
32
33
* @author 翟永超
33
34
* Create date:2017/8/7.
@@ -49,15 +50,20 @@ public SwaggerProperties swaggerProperties() {
49
50
50
51
@ Bean
51
52
public UiConfiguration uiConfiguration (SwaggerProperties swaggerProperties ) {
52
- return new UiConfiguration (
53
- swaggerProperties .getUiConfig ().getValidatorUrl (),// url
54
- swaggerProperties .getUiConfig ().getDocExpansion (), // docExpansion => none | list
55
- swaggerProperties .getUiConfig ().getApiSorter (), // apiSorter => alpha
56
- swaggerProperties .getUiConfig ().getDefaultModelRendering (), // defaultModelRendering => schema
57
- swaggerProperties .getUiConfig ().getSubmitMethods ().split ("," ),
58
- swaggerProperties .getUiConfig ().getJsonEditor (), // enableJsonEditor => true | false
59
- swaggerProperties .getUiConfig ().getShowRequestHeaders (), // showRequestHeaders => true | false
60
- 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 ();
61
67
}
62
68
63
69
@ Bean
@@ -93,14 +99,16 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
93
99
}
94
100
95
101
// exclude-path处理
96
- List <Predicate <String >> excludePath = new ArrayList ();
102
+ List <Predicate <String >> excludePath = new ArrayList <> ();
97
103
for (String path : swaggerProperties .getExcludePath ()) {
98
104
excludePath .add (PathSelectors .ant (path ));
99
105
}
100
106
101
107
Docket docketForBuilder = new Docket (DocumentationType .SWAGGER_2 )
102
108
.host (swaggerProperties .getHost ())
103
109
.apiInfo (apiInfo )
110
+ .securitySchemes (Collections .singletonList (apiKey ()))
111
+ .securityContexts (Collections .singletonList (securityContext ()))
104
112
.globalOperationParameters (buildGlobalOperationParametersFromSwaggerProperties (
105
113
swaggerProperties .getGlobalOperationParameters ()));
106
114
@@ -118,9 +126,9 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
118
126
)
119
127
).build ();
120
128
121
- /** ignoredParameterTypes **/
122
- Class [] array = new Class [swaggerProperties .getIgnoredParameterTypes ().size ()];
123
- Class [] ignoredParameterTypes = swaggerProperties .getIgnoredParameterTypes ().toArray (array );
129
+ /* ignoredParameterTypes **/
130
+ Class <?> [] array = new Class [swaggerProperties .getIgnoredParameterTypes ().size ()];
131
+ Class <?> [] ignoredParameterTypes = swaggerProperties .getIgnoredParameterTypes ().toArray (array );
124
132
docket .ignoredParameterTypes (ignoredParameterTypes );
125
133
126
134
configurableBeanFactory .registerSingleton ("defaultDocket" , docket );
@@ -167,6 +175,8 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
167
175
Docket docketForBuilder = new Docket (DocumentationType .SWAGGER_2 )
168
176
.host (swaggerProperties .getHost ())
169
177
.apiInfo (apiInfo )
178
+ .securitySchemes (Collections .singletonList (apiKey ()))
179
+ .securityContexts (Collections .singletonList (securityContext ()))
170
180
.globalOperationParameters (assemblyGlobalOperationParameters (swaggerProperties .getGlobalOperationParameters (),
171
181
docketInfo .getGlobalOperationParameters ()));
172
182
@@ -186,9 +196,9 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
186
196
)
187
197
.build ();
188
198
189
- /** ignoredParameterTypes **/
190
- Class [] array = new Class [docketInfo .getIgnoredParameterTypes ().size ()];
191
- Class [] ignoredParameterTypes = docketInfo .getIgnoredParameterTypes ().toArray (array );
199
+ /* ignoredParameterTypes **/
200
+ Class <?> [] array = new Class [docketInfo .getIgnoredParameterTypes ().size ()];
201
+ Class <?> [] ignoredParameterTypes = docketInfo .getIgnoredParameterTypes ().toArray (array );
192
202
docket .ignoredParameterTypes (ignoredParameterTypes );
193
203
194
204
configurableBeanFactory .registerSingleton (groupName , docket );
@@ -197,6 +207,44 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
197
207
return docketList ;
198
208
}
199
209
210
+ /**
211
+ * 配置基于 ApiKey 的鉴权对象
212
+ *
213
+ * @return
214
+ */
215
+ private ApiKey apiKey () {
216
+ return new ApiKey (swaggerProperties ().getAuthorization ().getName (),
217
+ swaggerProperties ().getAuthorization ().getKeyName (),
218
+ ApiKeyVehicle .HEADER .getValue ());
219
+ }
220
+
221
+ /**
222
+ * 配置默认的全局鉴权策略的开关,以及通过正则表达式进行匹配;默认 ^.*$ 匹配所有URL
223
+ * 其中 securityReferences 为配置启用的鉴权策略
224
+ *
225
+ * @return
226
+ */
227
+ private SecurityContext securityContext () {
228
+ return SecurityContext .builder ()
229
+ .securityReferences (defaultAuth ())
230
+ .forPaths (PathSelectors .regex (swaggerProperties ().getAuthorization ().getAuthRegex ()))
231
+ .build ();
232
+ }
233
+
234
+ /**
235
+ * 配置默认的全局鉴权策略;其中返回的 SecurityReference 中,reference 即为ApiKey对象里面的name,保持一致才能开启全局鉴权
236
+ *
237
+ * @return
238
+ */
239
+ private List <SecurityReference > defaultAuth () {
240
+ AuthorizationScope authorizationScope = new AuthorizationScope ("global" , "accessEverything" );
241
+ AuthorizationScope [] authorizationScopes = new AuthorizationScope [1 ];
242
+ authorizationScopes [0 ] = authorizationScope ;
243
+ return Collections .singletonList (SecurityReference .builder ()
244
+ .reference (swaggerProperties ().getAuthorization ().getName ())
245
+ .scopes (authorizationScopes ).build ());
246
+ }
247
+
200
248
201
249
@ Override
202
250
public void setBeanFactory (BeanFactory beanFactory ) throws BeansException {
@@ -206,7 +254,7 @@ public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
206
254
207
255
private List <Parameter > buildGlobalOperationParametersFromSwaggerProperties (
208
256
List <SwaggerProperties .GlobalOperationParameter > globalOperationParameters ) {
209
- List <Parameter > parameters = Lists . newArrayList ();
257
+ List <Parameter > parameters = newArrayList ();
210
258
211
259
if (Objects .isNull (globalOperationParameters )) {
212
260
return parameters ;
@@ -242,7 +290,7 @@ private List<Parameter> assemblyGlobalOperationParameters(
242
290
.map (SwaggerProperties .GlobalOperationParameter ::getName )
243
291
.collect (Collectors .toSet ());
244
292
245
- List <SwaggerProperties .GlobalOperationParameter > resultOperationParameters = Lists . newArrayList ();
293
+ List <SwaggerProperties .GlobalOperationParameter > resultOperationParameters = newArrayList ();
246
294
247
295
if (Objects .nonNull (globalOperationParameters )) {
248
296
for (SwaggerProperties .GlobalOperationParameter parameter : globalOperationParameters ) {
@@ -259,42 +307,43 @@ private List<Parameter> assemblyGlobalOperationParameters(
259
307
/**
260
308
* 设置全局响应消息
261
309
*
262
- * @param swaggerProperties 支持 POST,GET,PUT,PATCH,DELETE,HEAD,OPTIONS,TRACE
263
- * @param docketForBuilder
310
+ * @param swaggerProperties swaggerProperties 支持 POST,GET,PUT,PATCH,DELETE,HEAD,OPTIONS,TRACE
311
+ * @param docketForBuilder swagger docket builder
264
312
*/
265
313
private void buildGlobalResponseMessage (SwaggerProperties swaggerProperties , Docket docketForBuilder ) {
266
314
267
315
SwaggerProperties .GlobalResponseMessage globalResponseMessages =
268
316
swaggerProperties .getGlobalResponseMessage ();
269
317
270
- // POST,GET,PUT,PATCH,DELETE,HEAD,OPTIONS,TRACE 响应消息体
271
- List <ResponseMessage > postResponseMessages = getResponseMessageList (globalResponseMessages .getPost ());
272
- List <ResponseMessage > getResponseMessages = getResponseMessageList (globalResponseMessages .getGet ());
273
- List <ResponseMessage > putResponseMessages = getResponseMessageList (globalResponseMessages .getPut ());
274
- List <ResponseMessage > patchResponseMessages = getResponseMessageList (globalResponseMessages .getPatch ());
275
- List <ResponseMessage > deleteResponseMessages = getResponseMessageList (globalResponseMessages .getDelete ());
276
- List <ResponseMessage > headResponseMessages = getResponseMessageList (globalResponseMessages .getHead ());
277
- List <ResponseMessage > optionsResponseMessages = getResponseMessageList (globalResponseMessages .getOptions ());
278
- List <ResponseMessage > trackResponseMessages = getResponseMessageList (globalResponseMessages .getTrace ());
318
+ /* POST,GET,PUT,PATCH,DELETE,HEAD,OPTIONS,TRACE 响应消息体 **/
319
+ List <ResponseMessage > postResponseMessages = getResponseMessageList (globalResponseMessages .getPost ());
320
+ List <ResponseMessage > getResponseMessages = getResponseMessageList (globalResponseMessages .getGet ());
321
+ List <ResponseMessage > putResponseMessages = getResponseMessageList (globalResponseMessages .getPut ());
322
+ List <ResponseMessage > patchResponseMessages = getResponseMessageList (globalResponseMessages .getPatch ());
323
+ List <ResponseMessage > deleteResponseMessages = getResponseMessageList (globalResponseMessages .getDelete ());
324
+ List <ResponseMessage > headResponseMessages = getResponseMessageList (globalResponseMessages .getHead ());
325
+ List <ResponseMessage > optionsResponseMessages = getResponseMessageList (globalResponseMessages .getOptions ());
326
+ List <ResponseMessage > trackResponseMessages = getResponseMessageList (globalResponseMessages .getTrace ());
279
327
280
328
docketForBuilder .useDefaultResponseMessages (swaggerProperties .getApplyDefaultResponseMessages ())
281
- .globalResponseMessage (RequestMethod .POST , postResponseMessages )
282
- .globalResponseMessage (RequestMethod .GET , getResponseMessages )
283
- .globalResponseMessage (RequestMethod .PUT , putResponseMessages )
284
- .globalResponseMessage (RequestMethod .PATCH , patchResponseMessages )
285
- .globalResponseMessage (RequestMethod .DELETE , deleteResponseMessages )
286
- .globalResponseMessage (RequestMethod .HEAD , headResponseMessages )
287
- .globalResponseMessage (RequestMethod .OPTIONS , optionsResponseMessages )
288
- .globalResponseMessage (RequestMethod .TRACE , trackResponseMessages );
329
+ .globalResponseMessage (RequestMethod .POST , postResponseMessages )
330
+ .globalResponseMessage (RequestMethod .GET , getResponseMessages )
331
+ .globalResponseMessage (RequestMethod .PUT , putResponseMessages )
332
+ .globalResponseMessage (RequestMethod .PATCH , patchResponseMessages )
333
+ .globalResponseMessage (RequestMethod .DELETE , deleteResponseMessages )
334
+ .globalResponseMessage (RequestMethod .HEAD , headResponseMessages )
335
+ .globalResponseMessage (RequestMethod .OPTIONS , optionsResponseMessages )
336
+ .globalResponseMessage (RequestMethod .TRACE , trackResponseMessages );
289
337
}
290
338
291
339
/**
292
340
* 获取返回消息体列表
293
341
*
294
- * @param globalResponseMessageBodyList
342
+ * @param globalResponseMessageBodyList 全局Code消息返回集合
295
343
* @return
296
344
*/
297
- private List <ResponseMessage > getResponseMessageList (List <SwaggerProperties .GlobalResponseMessageBody > globalResponseMessageBodyList ) {
345
+ private List <ResponseMessage > getResponseMessageList
346
+ (List <SwaggerProperties .GlobalResponseMessageBody > globalResponseMessageBodyList ) {
298
347
List <ResponseMessage > responseMessages = new ArrayList <>();
299
348
for (SwaggerProperties .GlobalResponseMessageBody globalResponseMessageBody : globalResponseMessageBodyList ) {
300
349
ResponseMessageBuilder responseMessageBuilder = new ResponseMessageBuilder ();
0 commit comments