Skip to content

Commit

Permalink
1.7.0 更新,来自2018年的版本升级,欢呼吧,Coder们
Browse files Browse the repository at this point in the history
  • Loading branch information
gumutianqi committed Mar 21, 2018
1 parent 7bc3473 commit cc7bff7
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 7 deletions.
98 changes: 95 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,25 @@
# 版本基础

- Spring Boot:1.5.x
- Swagger:2.7.x
- Swagger:2.8.x

# 如何使用

在该项目的帮助下,我们的Spring Boot可以轻松的引入swagger2,主需要做下面两个步骤:

-`pom.xml`中引入依赖:

> 当前最新版本 1.7.0.RELEASE
```xml
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.6.0.RELEASE</version>
<version>1.7.0.RELEASE</version>
</dependency>
```

**注意:从`1.6.0`开始,我们按Spring Boot官方建议修改了artifactId为`swagger-spring-boot-starter`,1.6.0之前的版本不做修改,依然为使用`spring-boot-starter-swagger` !**
**注意:从`1.6.1`开始,我们按Spring Boot官方建议修改了artifactId为`swagger-spring-boot-starter`,1.6.0之前的版本不做修改,依然为使用`spring-boot-starter-swagger` !**

- 在应用主类中增加`@EnableSwagger2Doc`注解

Expand Down Expand Up @@ -249,13 +251,102 @@ swagger.ui-config.submit-methods=get,delete
swagger.ui-config.submit-methods=
```

---

### 来自2018年的版本升级,欢呼吧,Coder们

> 2018-03-21 今日春分,细雨如风 `1.7.0` 版本诞生 @gumutianqi
#### UI升级到 2.8.0 版本 (1.7.0 + 支持)

- 扁平化设计
- 更加华丽
- 更加易用
- 可配置项更加自由


### Authorization 鉴权配置 (1.7.0 + 支持)

- 新增 Authorization 配置项

```properties
# 鉴权策略ID,对应 SecurityReferences ID
swagger.authorization.name=Authorization

# 鉴权传递的Header参数
swagger.authorization.key-name=token

# 需要开启鉴权URL的正则, 默认^.*$匹配所有URL
swagger.authorization.auth-regex=^.*$
```

备注:目前支持`ApiKey`鉴权模式,后续添加`Oauth2``BasicAuth`支持

##### 使用须知

> 1. 默认已经在全局开启了`global`的SecurityReferences,无需配置任何参数就可以使用;
> 2. 全局鉴权的范围在可以通过以上参数`auth-regex`进行正则表达式匹配控制;
> 3. 除了全局开启外,还可以手动通过注解在RestController上进行定义鉴权,使用方式如下:
```java
// 其中的ID Authorization 即为配置项 swagger.authorization.name,详细请关注后面的配置代码
@ApiOperation(value = "Hello World", authorizations = {@Authorization(value = "Authorization")})
@RequestMapping(value = "/hello", method = RequestMethod.GET)
String hello();
```

##### 关于如何配置实现鉴权,请关注以下code:

```java
/**
* 配置基于 ApiKey 的鉴权对象
*
* @return
*/
private ApiKey apiKey() {
return new ApiKey(swaggerProperties().getAuthorization().getName(),
swaggerProperties().getAuthorization().getKeyName(),
ApiKeyVehicle.HEADER.getValue());
}

/**
* 配置默认的全局鉴权策略的开关,以及通过正则表达式进行匹配;默认 ^.*$ 匹配所有URL
* 其中 securityReferences 为配置启用的鉴权策略
*
* @return
*/
private SecurityContext securityContext() {
return SecurityContext.builder()
.securityReferences(defaultAuth())
.forPaths(PathSelectors.regex(swaggerProperties().getAuthorization().getAuthRegex()))
.build();
}

/**
* 配置默认的全局鉴权策略;其中返回的 SecurityReference 中,reference 即为ApiKey对象里面的name,保持一致才能开启全局鉴权
*
* @return
*/
private List<SecurityReference> defaultAuth() {
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
return Collections.singletonList(SecurityReference.builder()
.reference(swaggerProperties().getAuthorization().getName())
.scopes(authorizationScopes).build());
}
```


- 其他配置

```properties
# json编辑器
swagger.ui-config.json-editor=false

# 显示请求头
swagger.ui-config.show-request-headers=true

# 页面调试请求的超时时间
swagger.ui-config.request-timeout=5000
```
Expand All @@ -281,3 +372,4 @@ swagger.docket.aaa.ignored-parameter-types[1]=com.didispace.demo.Product
- [程序猿DD-翟永超](https://github.com/dyc87112/)
- [小火](https://renlulu.github.io/)
- [泥瓦匠BYSocket](https://github.com/JeffLi1993)
- [LarryKoo-古拉里](https://github.com/gumutianqi)
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.6.1.RELEASE</version>
<version>1.7.0.RELEASE</version>

<name>spring-boot-starter-swagger</name>
<url>https://github.com/SpringForAll/spring-boot-starter-swagger</url>
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/spring4all/swagger/SwaggerProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -340,17 +340,17 @@ public static class UiConfig {
static class Authorization {

/**
* 鉴权 API-KEY 名称标识
* 鉴权策略ID,对应 SecurityReferences ID
*/
private String name = "Authorization";

/**
* 鉴权参数
* 鉴权传递的Header参数
*/
private String keyName = "TOKEN";

/**
* 通过正则设置需要传递Authorization信息的API接口
* 需要开启鉴权URL的正则
*/
private String authRegex = "^.*$";
}
Expand Down

0 comments on commit cc7bff7

Please sign in to comment.