提交 2daffc3a 编写于 作者: 如梦技术's avatar 如梦技术 🐛

mica-swagger 添加 SwaggerCustomizer,用于自定义配置。

上级 80911e91
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
## 发行版本 ## 发行版本
### v2.1.0-GA - 2020-12-13 ### v2.1.0-GA - 2020-12-13
- :sparkles: mica-redis 添加 redis key 失效事件配置。 - :sparkles: mica-redis 添加 redis key 失效事件配置。
- :sparkles: mica-swagger 添加 SwaggerCustomizer,用于自定义配置。
- :sparkles: mica-swagger 添加 oauth2 认证配置。 - :sparkles: mica-swagger 添加 oauth2 认证配置。
- :sparkles: mica-http 添加流上传,完善文档。 - :sparkles: mica-http 添加流上传,完善文档。
- :sparkles: mica-core 添加 GeoUtil。 - :sparkles: mica-core 添加 GeoUtil。
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package net.dreamlu.mica.swagger; package net.dreamlu.mica.swagger.config;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
......
...@@ -14,12 +14,13 @@ ...@@ -14,12 +14,13 @@
* limitations under the License. * limitations under the License.
*/ */
package net.dreamlu.mica.swagger; package net.dreamlu.mica.swagger.config;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import net.dreamlu.mica.swagger.MicaSwaggerProperties.Authorization; import net.dreamlu.mica.swagger.config.MicaSwaggerProperties.Authorization;
import net.dreamlu.mica.swagger.MicaSwaggerProperties.GrantTypes; import net.dreamlu.mica.swagger.config.MicaSwaggerProperties.GrantTypes;
import net.dreamlu.mica.swagger.MicaSwaggerProperties.Oauth2; import net.dreamlu.mica.swagger.config.MicaSwaggerProperties.Oauth2;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
...@@ -56,8 +57,9 @@ public class SwaggerConfiguration { ...@@ -56,8 +57,9 @@ public class SwaggerConfiguration {
@Bean @Bean
public Docket docket(Environment environment, public Docket docket(Environment environment,
MicaSwaggerProperties properties) { MicaSwaggerProperties properties,
// 组名为应用名 ObjectProvider<List<SwaggerCustomizer>> swaggerCustomizersProvider) {
// 1. 组名为应用名
String appName = environment.getProperty("spring.application.name"); String appName = environment.getProperty("spring.application.name");
Docket docket = new Docket(DocumentationType.SWAGGER_2) Docket docket = new Docket(DocumentationType.SWAGGER_2)
.useDefaultResponseMessages(false) .useDefaultResponseMessages(false)
...@@ -66,18 +68,20 @@ public class SwaggerConfiguration { ...@@ -66,18 +68,20 @@ public class SwaggerConfiguration {
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
.paths(PathSelectors.any()) .paths(PathSelectors.any())
.build(); .build();
// 如果开启 apiKey 认证 // 2. 如果开启 apiKey 认证
if (properties.getAuthorization().getEnabled()) { if (properties.getAuthorization().getEnabled()) {
Authorization authorization = properties.getAuthorization(); Authorization authorization = properties.getAuthorization();
docket.securitySchemes(Collections.singletonList(apiKey(authorization))); docket.securitySchemes(Collections.singletonList(apiKey(authorization)));
docket.securityContexts(Collections.singletonList(apiKeySecurityContext(authorization))); docket.securityContexts(Collections.singletonList(apiKeySecurityContext(authorization)));
} }
// 如果开启 oauth2 认证 // 3. 如果开启 oauth2 认证
if (properties.getOauth2().getEnabled()) { if (properties.getOauth2().getEnabled()) {
Oauth2 oauth2 = properties.getOauth2(); Oauth2 oauth2 = properties.getOauth2();
docket.securitySchemes(Collections.singletonList(oauth2(oauth2))); docket.securitySchemes(Collections.singletonList(oauth2(oauth2)));
docket.securityContexts(Collections.singletonList(oauth2SecurityContext(oauth2))); docket.securityContexts(Collections.singletonList(oauth2SecurityContext(oauth2)));
} }
// 4. 自定义 customizer 配置
swaggerCustomizersProvider.ifAvailable(customizers -> customizers.forEach(customizer -> customizer.customize(docket)));
return docket; return docket;
} }
......
/*
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dreamlu.mica.swagger.config;
import springfox.documentation.spring.web.plugins.Docket;
/**
* swagger 自定义配置
*
* @author L.cm
*/
@FunctionalInterface
public interface SwaggerCustomizer {
/**
* Customize the Swagger Docket.
*
* @param docket the Docket to customize
*/
void customize(Docket docket);
}
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
@NonNullApi @NonNullApi
@NonNullFields @NonNullFields
package net.dreamlu.mica.swagger; package net.dreamlu.mica.swagger.config;
import org.springframework.lang.NonNullApi; import org.springframework.lang.NonNullApi;
import org.springframework.lang.NonNullFields; import org.springframework.lang.NonNullFields;
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册