1616# 版本基础
1717
1818- Spring Boot:1.5.x
19- - Swagger:2.7 .x
19+ - Swagger:2.8 .x
2020
2121# 如何使用
2222
2323在该项目的帮助下,我们的Spring Boot可以轻松的引入swagger2,主需要做下面两个步骤:
2424
2525- 在` pom.xml ` 中引入依赖:
2626
27+ > 当前最新版本 1.7.0.RELEASE
28+
2729``` xml
2830<dependency >
2931 <groupId >com.spring4all</groupId >
3032 <artifactId >swagger-spring-boot-starter</artifactId >
31- <version >1.6 .0.RELEASE</version >
33+ <version >1.7 .0.RELEASE</version >
3234</dependency >
3335```
3436
35- ** 注意:从` 1.6.0 ` 开始,我们按Spring Boot官方建议修改了artifactId为` swagger-spring-boot-starter ` ,1.6.0之前的版本不做修改,依然为使用` spring-boot-starter-swagger ` !**
37+ ** 注意:从` 1.6.1 ` 开始,我们按Spring Boot官方建议修改了artifactId为` swagger-spring-boot-starter ` ,1.6.0之前的版本不做修改,依然为使用` spring-boot-starter-swagger ` !**
3638
3739- 在应用主类中增加` @EnableSwagger2Doc ` 注解
3840
@@ -249,13 +251,102 @@ swagger.ui-config.submit-methods=get,delete
249251swagger.ui-config.submit-methods =
250252```
251253
254+ ---
255+
256+ ### 来自2018年的版本升级,欢呼吧,Coder们
257+
258+ > 2018-03-21 今日春分,细雨如风 ` 1.7.0 ` 版本诞生 @gumutianqi
259+
260+ #### UI升级到 2.8.0 版本 (1.7.0 + 支持)
261+
262+ - 扁平化设计
263+ - 更加华丽
264+ - 更加易用
265+ - 可配置项更加自由
266+
267+
268+ ### Authorization 鉴权配置 (1.7.0 + 支持)
269+
270+ - 新增 Authorization 配置项
271+
272+ ``` properties
273+ # 鉴权策略ID,对应 SecurityReferences ID
274+ swagger.authorization.name =Authorization
275+
276+ # 鉴权传递的Header参数
277+ swagger.authorization.key-name =token
278+
279+ # 需要开启鉴权URL的正则, 默认^.*$匹配所有URL
280+ swagger.authorization.auth-regex =^.*$
281+ ```
282+
283+ 备注:目前支持` ApiKey ` 鉴权模式,后续添加` Oauth2 ` 和` BasicAuth ` 支持
284+
285+ ##### 使用须知
286+
287+ > 1 . 默认已经在全局开启了` global ` 的SecurityReferences,无需配置任何参数就可以使用;
288+ > 2 . 全局鉴权的范围在可以通过以上参数` auth-regex ` 进行正则表达式匹配控制;
289+ > 3 . 除了全局开启外,还可以手动通过注解在RestController上进行定义鉴权,使用方式如下:
290+
291+ ``` java
292+ // 其中的ID Authorization 即为配置项 swagger.authorization.name,详细请关注后面的配置代码
293+ @ApiOperation (value = " Hello World" , authorizations = {@Authorization (value = " Authorization" )})
294+ @RequestMapping (value = " /hello" , method = RequestMethod . GET )
295+ String hello();
296+ ```
297+
298+ ##### 关于如何配置实现鉴权,请关注以下code:
299+
300+ ``` java
301+ /**
302+ * 配置基于 ApiKey 的鉴权对象
303+ *
304+ * @return
305+ */
306+ private ApiKey apiKey() {
307+ return new ApiKey (swaggerProperties(). getAuthorization(). getName(),
308+ swaggerProperties(). getAuthorization(). getKeyName(),
309+ ApiKeyVehicle . HEADER. getValue());
310+ }
311+
312+ /**
313+ * 配置默认的全局鉴权策略的开关,以及通过正则表达式进行匹配;默认 ^.*$ 匹配所有URL
314+ * 其中 securityReferences 为配置启用的鉴权策略
315+ *
316+ * @return
317+ */
318+ private SecurityContext securityContext() {
319+ return SecurityContext . builder()
320+ .securityReferences(defaultAuth())
321+ .forPaths(PathSelectors . regex(swaggerProperties(). getAuthorization(). getAuthRegex()))
322+ .build();
323+ }
324+
325+ /**
326+ * 配置默认的全局鉴权策略;其中返回的 SecurityReference 中,reference 即为ApiKey对象里面的name,保持一致才能开启全局鉴权
327+ *
328+ * @return
329+ */
330+ private List<SecurityReference > defaultAuth() {
331+ AuthorizationScope authorizationScope = new AuthorizationScope (" global" , " accessEverything" );
332+ AuthorizationScope [] authorizationScopes = new AuthorizationScope [1 ];
333+ authorizationScopes[0 ] = authorizationScope;
334+ return Collections . singletonList(SecurityReference . builder()
335+ .reference(swaggerProperties(). getAuthorization(). getName())
336+ .scopes(authorizationScopes). build());
337+ }
338+ ```
339+
340+
252341- 其他配置
253342
254343``` properties
255344# json编辑器
256345swagger.ui-config.json-editor =false
346+
257347# 显示请求头
258348swagger.ui-config.show-request-headers =true
349+
259350# 页面调试请求的超时时间
260351swagger.ui-config.request-timeout =5000
261352```
@@ -281,3 +372,4 @@ swagger.docket.aaa.ignored-parameter-types[1]=com.didispace.demo.Product
281372- [ 程序猿DD-翟永超] ( https://github.com/dyc87112/ )
282373- [ 小火] ( https://renlulu.github.io/ )
283374- [ 泥瓦匠BYSocket] ( https://github.com/JeffLi1993 )
375+ - [ LarryKoo-古拉里] ( https://github.com/gumutianqi )
0 commit comments