Skip to content

Commit 7bc3473

Browse files
committed
1. 升级spring-boot为1.5.10的稳定版本;
2. 优化springfox在2.8版本更新后废弃的方法; 3. 核心升级主要是终于修复了SecuritySchemes和SecurityContexts在这个版本的可用性,已经默认开启ApiKey模式,可自由配置; 3.1 默认ApiKey("Authorization", "token", "^.*$"),分别为鉴权策略ID,鉴权是header的参数名,和需要开启鉴权URL的正则;
1 parent d7de400 commit 7bc3473

File tree

3 files changed

+93
-51
lines changed

3 files changed

+93
-51
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
5050
<version.java>1.8</version.java>
5151
<version.swagger>2.8.0</version.swagger>
52-
<version.spring-boot>1.5.9.RELEASE</version.spring-boot>
52+
<version.spring-boot>1.5.10.RELEASE</version.spring-boot>
5353
<version.lombok>1.16.18</version.lombok>
5454
</properties>
5555

src/main/java/com/spring4all/swagger/SwaggerAutoConfiguration.java

Lines changed: 54 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import springfox.documentation.spi.DocumentationType;
2121
import springfox.documentation.spi.service.contexts.SecurityContext;
2222
import springfox.documentation.spring.web.plugins.Docket;
23+
import springfox.documentation.swagger.web.ApiKeyVehicle;
2324
import springfox.documentation.swagger.web.UiConfiguration;
25+
import springfox.documentation.swagger.web.UiConfigurationBuilder;
2426

2527
import java.util.*;
2628
import 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();

src/main/java/com/spring4all/swagger/SwaggerProperties.java

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import lombok.Data;
44
import lombok.NoArgsConstructor;
55
import org.springframework.boot.context.properties.ConfigurationProperties;
6+
import springfox.documentation.swagger.web.DocExpansion;
7+
import springfox.documentation.swagger.web.ModelRendering;
8+
import springfox.documentation.swagger.web.OperationsSorter;
9+
import springfox.documentation.swagger.web.TagsSorter;
610

711
import java.util.ArrayList;
812
import java.util.LinkedHashMap;
@@ -51,7 +55,7 @@ public class SwaggerProperties {
5155
/**
5256
* 忽略的参数类型
5357
**/
54-
private List<Class> ignoredParameterTypes = new ArrayList<>();
58+
private List<Class<?>> ignoredParameterTypes = new ArrayList<>();
5559

5660
private Contact contact = new Contact();
5761

@@ -184,7 +188,7 @@ public static class DocketInfo {
184188
/**
185189
* 忽略的参数类型
186190
**/
187-
private List<Class> ignoredParameterTypes = new ArrayList<>();
191+
private List<Class<?>> ignoredParameterTypes = new ArrayList<>();
188192

189193
}
190194

@@ -279,10 +283,8 @@ public static class GlobalResponseMessageBody {
279283
@NoArgsConstructor
280284
public static class UiConfig {
281285

282-
private String validatorUrl;
283-
private String docExpansion = "none"; // none | list
284-
private String apiSorter = "alpha"; // alpha
285-
private String defaultModelRendering = "schema"; // schema
286+
287+
private String apiSorter = "alpha";
286288

287289
/**
288290
* 是否启用json编辑器
@@ -301,24 +303,51 @@ public static class UiConfig {
301303
**/
302304
private Long requestTimeout = 10000L;
303305

306+
private Boolean deepLinking;
307+
private Boolean displayOperationId;
308+
private Integer defaultModelsExpandDepth;
309+
private Integer defaultModelExpandDepth;
310+
private ModelRendering defaultModelRendering;
311+
312+
/**
313+
* 是否显示请求耗时,默认false
314+
*/
315+
private Boolean displayRequestDuration = true;
316+
/**
317+
* 可选 none | list
318+
*/
319+
private DocExpansion docExpansion;
320+
/**
321+
* Boolean=false OR String
322+
*/
323+
private Object filter;
324+
private Integer maxDisplayedTags;
325+
private OperationsSorter operationsSorter;
326+
private Boolean showExtensions;
327+
private TagsSorter tagsSorter;
328+
329+
/**
330+
* Network
331+
*/
332+
private String validatorUrl;
304333
}
305334

306335
/**
307336
* securitySchemes 支持方式之一 ApiKey
308337
*/
309338
@Data
310339
@NoArgsConstructor
311-
public static class Authorization {
340+
static class Authorization {
312341

313342
/**
314343
* 鉴权 API-KEY 名称标识
315344
*/
316-
private String name = "TOKEN";
345+
private String name = "Authorization";
317346

318347
/**
319348
* 鉴权参数
320349
*/
321-
private String keyName = "API-TOKEN";
350+
private String keyName = "TOKEN";
322351

323352
/**
324353
* 通过正则设置需要传递Authorization信息的API接口

0 commit comments

Comments
 (0)