diff --git a/README.md b/README.md index 379d966e6b619ef6b03b9b9835affaae20d567a2..0a9d41239a1f8c784a1a689ec4e68fc50055d8b0 100644 --- a/README.md +++ b/README.md @@ -13,8 +13,7 @@ # 版本基础 -- Spring Boot:1.5.x -- Swagger:2.8.x +- Swagger:2.9.2 # 如何使用 @@ -22,13 +21,11 @@ - 在`pom.xml`中引入依赖: -> 当前最新版本 1.7.0.RELEASE - ```xml com.spring4all swagger-spring-boot-starter - 1.7.0.RELEASE + 1.9.0.RELEASE ``` @@ -286,6 +283,9 @@ 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 @@ -293,7 +293,7 @@ swagger.authorization.key-name=token swagger.authorization.auth-regex=^.*$ ``` -备注:目前支持`ApiKey`鉴权模式,后续添加`Oauth2`和`BasicAuth`支持 +备注:目前支持`ApiKey` | `BasicAuth`鉴权模式,`None`除消鉴权模式,默认ApiKey,后续添加`Oauth2`支持 **使用须知** diff --git a/pom.xml b/pom.xml index 2a249ca8c99c77aff2fe41697c31e510c1f77224..5f3789fd317d5246f648922b2e96d85bb612349d 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.spring4all swagger-spring-boot-starter - 1.7.0.RELEASE + 1.9.0.RELEASE spring-boot-starter-swagger https://github.com/SpringForAll/spring-boot-starter-swagger @@ -48,9 +48,9 @@ UTF-8 1.8 - 2.8.0 + 2.9.2 1.5.10.RELEASE - 1.16.18 + 1.18.6 @@ -64,6 +64,11 @@ spring-boot-configuration-processor true + + org.springframework.boot + spring-boot-starter-web + true + io.springfox springfox-swagger-ui @@ -85,11 +90,6 @@ ${version.lombok} provided - - org.springframework.boot - spring-boot-starter-web - true - @@ -119,4 +119,12 @@ + + + + jcenter-snapshots + jcenter + https://jcenter.bintray.com/ + + \ No newline at end of file diff --git a/src/main/java/com/spring4all/swagger/SwaggerAutoConfiguration.java b/src/main/java/com/spring4all/swagger/SwaggerAutoConfiguration.java index 143dd4e25680660cdd645836fc1349ec8b43773e..77d9e9f0e80bbf882d42ccb4d1ca29440db246f9 100644 --- a/src/main/java/com/spring4all/swagger/SwaggerAutoConfiguration.java +++ b/src/main/java/com/spring4all/swagger/SwaggerAutoConfiguration.java @@ -107,11 +107,16 @@ public class SwaggerAutoConfiguration implements BeanFactoryAware { 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); @@ -175,11 +180,16 @@ public class SwaggerAutoConfiguration implements BeanFactoryAware { 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); @@ -218,6 +228,15 @@ public class SwaggerAutoConfiguration implements BeanFactoryAware { ApiKeyVehicle.HEADER.getValue()); } + /** + * 配置基于 BasicAuth 的鉴权对象 + * + * @return + */ + private BasicAuth basicAuth() { + return new BasicAuth(swaggerProperties().getAuthorization().getName()); + } + /** * 配置默认的全局鉴权策略的开关,以及通过正则表达式进行匹配;默认 ^.*$ 匹配所有URL * 其中 securityReferences 为配置启用的鉴权策略 diff --git a/src/main/java/com/spring4all/swagger/SwaggerProperties.java b/src/main/java/com/spring4all/swagger/SwaggerProperties.java index 0a4314240c2a54ce1ff381be4f82d17edd6fca85..17768f650afa8d920e46c1a0fd159c0b9c66b87b 100644 --- a/src/main/java/com/spring4all/swagger/SwaggerProperties.java +++ b/src/main/java/com/spring4all/swagger/SwaggerProperties.java @@ -344,6 +344,11 @@ public class SwaggerProperties { */ private String name = "Authorization"; + /** + * 鉴权策略,可选 ApiKey | BasicAuth | None,默认ApiKey + */ + private String type = "ApiKey"; + /** * 鉴权传递的Header参数 */