Skip to content

支持 BasicAuth #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,17 @@ swagger.docket.aaa.ignored-parameter-types[1]=com.didispace.demo.Product
# 鉴权策略ID,对应 SecurityReferences ID
swagger.authorization.name=Authorization

# 鉴权策略,可选 ApiKey | BasicAuth | None,默认ApiKey
swagger.authorization.type=ApiKey

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

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

备注:目前支持`ApiKey`鉴权模式,后续添加`Oauth2`和`BasicAuth`支持
备注:目前支持`ApiKey` | `BasicAuth`鉴权模式,`None`除消鉴权模式,默认ApiKey,后续添加`Oauth2`支持

**使用须知**

Expand Down
23 changes: 21 additions & 2 deletions src/main/java/com/spring4all/swagger/SwaggerAutoConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,16 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
Docket docketForBuilder = new Docket(DocumentationType.SWAGGER_2)
.host(swaggerProperties.getHost())
.apiInfo(apiInfo)
.securitySchemes(Collections.singletonList(apiKey()))
.securityContexts(Collections.singletonList(securityContext()))
.globalOperationParameters(buildGlobalOperationParametersFromSwaggerProperties(
swaggerProperties.getGlobalOperationParameters()));

if ("BasicAuth".equalsIgnoreCase(swaggerProperties.getAuthorization().getType())) {
docketForBuilder.securitySchemes(Collections.singletonList(basicAuth()));
} else if (!"None".equalsIgnoreCase(swaggerProperties.getAuthorization().getType())) {
docketForBuilder.securitySchemes(Collections.singletonList(apiKey()));
}

// 全局响应消息
if (!swaggerProperties.getApplyDefaultResponseMessages()) {
buildGlobalResponseMessage(swaggerProperties, docketForBuilder);
Expand Down Expand Up @@ -175,11 +180,16 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
Docket docketForBuilder = new Docket(DocumentationType.SWAGGER_2)
.host(swaggerProperties.getHost())
.apiInfo(apiInfo)
.securitySchemes(Collections.singletonList(apiKey()))
.securityContexts(Collections.singletonList(securityContext()))
.globalOperationParameters(assemblyGlobalOperationParameters(swaggerProperties.getGlobalOperationParameters(),
docketInfo.getGlobalOperationParameters()));

if ("BasicAuth".equalsIgnoreCase(swaggerProperties.getAuthorization().getType())) {
docketForBuilder.securitySchemes(Collections.singletonList(basicAuth()));
} else if (!"None".equalsIgnoreCase(swaggerProperties.getAuthorization().getType())) {
docketForBuilder.securitySchemes(Collections.singletonList(apiKey()));
}

// 全局响应消息
if (!swaggerProperties.getApplyDefaultResponseMessages()) {
buildGlobalResponseMessage(swaggerProperties, docketForBuilder);
Expand Down Expand Up @@ -218,6 +228,15 @@ private ApiKey apiKey() {
ApiKeyVehicle.HEADER.getValue());
}

/**
* 配置基于 BasicAuth 的鉴权对象
*
* @return
*/
private BasicAuth basicAuth() {
return new BasicAuth(swaggerProperties().getAuthorization().getName());
}

/**
* 配置默认的全局鉴权策略的开关,以及通过正则表达式进行匹配;默认 ^.*$ 匹配所有URL
* 其中 securityReferences 为配置启用的鉴权策略
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/spring4all/swagger/SwaggerProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,11 @@ static class Authorization {
*/
private String name = "Authorization";

/**
* 鉴权策略,可选 ApiKey | BasicAuth | None,默认ApiKey
*/
private String type = "ApiKey";

/**
* 鉴权传递的Header参数
*/
Expand Down