2020import springfox .documentation .spi .DocumentationType ;
2121import springfox .documentation .spi .service .contexts .SecurityContext ;
2222import springfox .documentation .spring .web .plugins .Docket ;
23+ import springfox .documentation .swagger .web .ApiKeyVehicle ;
2324import springfox .documentation .swagger .web .UiConfiguration ;
25+ import springfox .documentation .swagger .web .UiConfigurationBuilder ;
2426
2527import java .util .*;
2628import java .util .stream .Collectors ;
@@ -48,15 +50,20 @@ public SwaggerProperties swaggerProperties() {
4850
4951 @ Bean
5052 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 ();
6067 }
6168
6269 @ Bean
@@ -92,16 +99,16 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
9299 }
93100
94101 // exclude-path处理
95- List <Predicate <String >> excludePath = new ArrayList ();
102+ List <Predicate <String >> excludePath = new ArrayList <> ();
96103 for (String path : swaggerProperties .getExcludePath ()) {
97104 excludePath .add (PathSelectors .ant (path ));
98105 }
99106
100107 Docket docketForBuilder = new Docket (DocumentationType .SWAGGER_2 )
101108 .host (swaggerProperties .getHost ())
102109 .apiInfo (apiInfo )
103- .securitySchemes (this . securitySchemes ( ))
104- .securityContexts (this . securityContexts ( ))
110+ .securitySchemes (Collections . singletonList ( apiKey () ))
111+ .securityContexts (Collections . singletonList ( securityContext () ))
105112 .globalOperationParameters (buildGlobalOperationParametersFromSwaggerProperties (
106113 swaggerProperties .getGlobalOperationParameters ()));
107114
@@ -119,9 +126,9 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
119126 )
120127 ).build ();
121128
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 );
125132 docket .ignoredParameterTypes (ignoredParameterTypes );
126133
127134 configurableBeanFactory .registerSingleton ("defaultDocket" , docket );
@@ -168,8 +175,8 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
168175 Docket docketForBuilder = new Docket (DocumentationType .SWAGGER_2 )
169176 .host (swaggerProperties .getHost ())
170177 .apiInfo (apiInfo )
171- .securitySchemes (this . securitySchemes ( ))
172- .securityContexts (this . securityContexts ( ))
178+ .securitySchemes (Collections . singletonList ( apiKey () ))
179+ .securityContexts (Collections . singletonList ( securityContext () ))
173180 .globalOperationParameters (assemblyGlobalOperationParameters (swaggerProperties .getGlobalOperationParameters (),
174181 docketInfo .getGlobalOperationParameters ()));
175182
@@ -189,9 +196,9 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
189196 )
190197 .build ();
191198
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 );
195202 docket .ignoredParameterTypes (ignoredParameterTypes );
196203
197204 configurableBeanFactory .registerSingleton (groupName , docket );
@@ -201,36 +208,41 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
201208 }
202209
203210 /**
204- * 配置 Authorization ApiKey
211+ * 配置基于 ApiKey 的鉴权对象
205212 *
206213 * @return
207214 */
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 ( ));
212219 }
213220
214221 /**
215- * 通过正则设置需要传递 Authorization 信息的API接口
222+ * 配置默认的全局鉴权策略的开关,以及通过正则表达式进行匹配;默认 ^.*$ 匹配所有URL
223+ * 其中 securityReferences 为配置启用的鉴权策略
216224 *
217225 * @return
218226 */
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 ();
226232 }
227233
228- List <SecurityReference > defaultAuth () {
234+ /**
235+ * 配置默认的全局鉴权策略;其中返回的 SecurityReference 中,reference 即为ApiKey对象里面的name,保持一致才能开启全局鉴权
236+ *
237+ * @return
238+ */
239+ private List <SecurityReference > defaultAuth () {
229240 AuthorizationScope authorizationScope = new AuthorizationScope ("global" , "accessEverything" );
230241 AuthorizationScope [] authorizationScopes = new AuthorizationScope [1 ];
231242 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 ());
234246 }
235247
236248
@@ -295,15 +307,15 @@ private List<Parameter> assemblyGlobalOperationParameters(
295307 /**
296308 * 设置全局响应消息
297309 *
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
300312 */
301313 private void buildGlobalResponseMessage (SwaggerProperties swaggerProperties , Docket docketForBuilder ) {
302314
303315 SwaggerProperties .GlobalResponseMessage globalResponseMessages =
304316 swaggerProperties .getGlobalResponseMessage ();
305317
306- // POST,GET,PUT,PATCH,DELETE,HEAD,OPTIONS,TRACE 响应消息体
318+ /* POST,GET,PUT,PATCH,DELETE,HEAD,OPTIONS,TRACE 响应消息体 **/
307319 List <ResponseMessage > postResponseMessages = getResponseMessageList (globalResponseMessages .getPost ());
308320 List <ResponseMessage > getResponseMessages = getResponseMessageList (globalResponseMessages .getGet ());
309321 List <ResponseMessage > putResponseMessages = getResponseMessageList (globalResponseMessages .getPut ());
@@ -327,10 +339,11 @@ private void buildGlobalResponseMessage(SwaggerProperties swaggerProperties, Doc
327339 /**
328340 * 获取返回消息体列表
329341 *
330- * @param globalResponseMessageBodyList
342+ * @param globalResponseMessageBodyList 全局Code消息返回集合
331343 * @return
332344 */
333- private List <ResponseMessage > getResponseMessageList (List <SwaggerProperties .GlobalResponseMessageBody > globalResponseMessageBodyList ) {
345+ private List <ResponseMessage > getResponseMessageList
346+ (List <SwaggerProperties .GlobalResponseMessageBody > globalResponseMessageBodyList ) {
334347 List <ResponseMessage > responseMessages = new ArrayList <>();
335348 for (SwaggerProperties .GlobalResponseMessageBody globalResponseMessageBody : globalResponseMessageBodyList ) {
336349 ResponseMessageBuilder responseMessageBuilder = new ResponseMessageBuilder ();
0 commit comments