11package org .minbox .framework .api .boot .autoconfigure .swagger ;
22
3+ import org .springframework .beans .BeanUtils ;
34import org .springframework .beans .factory .BeanFactory ;
45import org .springframework .boot .autoconfigure .AutoConfigurationPackages ;
56import org .springframework .boot .autoconfigure .condition .ConditionalOnClass ;
2223import springfox .documentation .swagger2 .configuration .Swagger2DocumentationConfiguration ;
2324import springfox .documentation .swagger2 .web .Swagger2Controller ;
2425
26+ import java .util .Arrays ;
2527import java .util .Collections ;
2628import java .util .List ;
29+ import java .util .stream .Collectors ;
30+ import java .util .stream .Stream ;
2731
2832import static org .minbox .framework .api .boot .autoconfigure .swagger .SwaggerProperties .API_BOOT_SWAGGER_PREFIX ;
2933
3034/**
31- * Swagger2自动化配置
32- * 只有在设置api .boot.swagger.enable=true时或者在配置文件内不进行配置时(因为默认为true)才会自动化配置该类
33- * 通过@Import导入Swagger原本配置类
35+ * Swagger2 automation configuration
36+ * Only when setting "api .boot.swagger.enable=true" or when no configuration in the configuration file (because the default is true) will automatically configure this class
37+ * Import the original configuration class of Swagger through @Import
3438 *
35- * @author:恒宇少年 - 于起宇
36- * <p>
37- * DateTime:2019-03-16 23:41
38- * Blog:http://blog.yuqiyu.com
39- * WebSite:http://www.jianshu.com/u/092df3f77bca
40- * Gitee:https://gitee.com/hengboy
41- * GitHub:https://github.com/hengboy
39+ * @author 恒宇少年
4240 */
4341@ Configuration
4442@ EnableConfigurationProperties (SwaggerProperties .class )
4543@ ConditionalOnClass ({SwaggerCommonConfiguration .class , Swagger2DocumentationConfiguration .class , Swagger2Controller .class })
4644@ ConditionalOnProperty (prefix = API_BOOT_SWAGGER_PREFIX , name = "enable" , havingValue = "true" , matchIfMissing = true )
4745@ Import ({Swagger2DocumentationConfiguration .class , BeanValidatorPluginsConfiguration .class })
4846public class ApiBootSwaggerAutoConfiguration {
49- /**
50- * swagger相关属性配置
51- */
47+ private static final String AUTHORIZATION_SCOPE = "global" ;
48+ private static final String AUTHORIZATION_SCOPE_DESCRIPTION = "accessEverything" ;
5249 private SwaggerProperties swaggerProperties ;
53- /**
54- * spring bean factory
55- */
5650 private BeanFactory beanFactory ;
5751
5852 public ApiBootSwaggerAutoConfiguration (SwaggerProperties swaggerProperties , BeanFactory beanFactory ) {
@@ -61,59 +55,97 @@ public ApiBootSwaggerAutoConfiguration(SwaggerProperties swaggerProperties, Bean
6155 }
6256
6357 /**
64- * 配置swagger基本信息
65- * - BasePackage
66- * 默认使用SpringBoot项目扫描bean根目录
67- * 如果存在配置时则使用SwaggerProperties.basePackage作为扫描根目录
58+ * Configure {@link Docket}
59+ * <p>
60+ * The base package value:
61+ * default value is {@link SwaggerProperties#getBasePackage()},if is it empty
62+ * the {@link AutoConfigurationPackages#get} is used
6863 *
69- * @return Docket实例
64+ * @return The {@link Docket} instance
7065 */
7166 @ Bean
7267 public Docket docket () {
7368 String basePackage = swaggerProperties .getBasePackage ();
7469 if (StringUtils .isEmpty (basePackage )) {
7570 basePackage = AutoConfigurationPackages .get (beanFactory ).get (0 );
7671 }
77- return new Docket (DocumentationType .SWAGGER_2 ).apiInfo (apiInfo ()).securitySchemes (Collections .singletonList (apiKey ())).securityContexts (Collections .singletonList (securityContext ())).select ().apis (RequestHandlerSelectors .basePackage (basePackage )).paths (PathSelectors .any ()).build ();
72+ return new Docket (DocumentationType .SWAGGER_2 )
73+ .apiInfo (apiInfo ())
74+ .securitySchemes (Collections .singletonList (apiKey ()))
75+ .securityContexts (Collections .singletonList (securityContext ()))
76+ .select ()
77+ .apis (RequestHandlerSelectors .basePackage (basePackage ))
78+ .paths (PathSelectors .any ())
79+ .build ();
7880 }
7981
8082 /**
81- * 配置文档基本信息
82- * 如:文档标题、版本、描述、联系人基本信息等
83+ * Configure {@link ApiInfo}
8384 *
84- * @return ApiInfo实例
85+ * @return The {@link ApiInfo} instance
8586 */
8687 public ApiInfo apiInfo () {
87- return new ApiInfoBuilder ().title (swaggerProperties .getTitle ()).description (swaggerProperties .getDescription ()).version (swaggerProperties .getVersion ()).license (swaggerProperties .getLicense ()).licenseUrl (swaggerProperties .getLicenseUrl ()).contact (new Contact (swaggerProperties .getContact ().getName (), swaggerProperties .getContact ().getWebsite (), swaggerProperties .getContact ().getEmail ())).build ();
88+ Contact contact = this .convertContact (swaggerProperties .getContact ());
89+ return new ApiInfoBuilder ()
90+ .title (swaggerProperties .getTitle ())
91+ .description (swaggerProperties .getDescription ())
92+ .version (swaggerProperties .getVersion ())
93+ .license (swaggerProperties .getLicense ())
94+ .licenseUrl (swaggerProperties .getLicenseUrl ())
95+ .contact (contact )
96+ .build ();
8897 }
8998
9099 /**
91- * 配置Swagger整合Oauth2时的请求Key信息
92- * 使用头部传递方式 Authorization: Beare TokenValue
100+ * Request key information when configuring Swagger to integrate Oauth2
101+ * <p>
102+ * Use header transfer {@link ApiKeyVehicle#HEADER} method "Authorization: Bearer TokenValue"
93103 *
94- * @return apiKey
104+ * @return The {@link ApiKey} instance
95105 */
96106 private ApiKey apiKey () {
97- return new ApiKey (swaggerProperties .getAuthorization ().getName (), swaggerProperties .getAuthorization ().getKeyName (), ApiKeyVehicle .HEADER .getValue ());
107+ return new ApiKey (swaggerProperties .getAuthorization ().getName (),
108+ swaggerProperties .getAuthorization ().getKeyName (),
109+ ApiKeyVehicle .HEADER .getValue ());
98110 }
99111
100112 /**
101- * 配置安全上下文
113+ * Configure {@link SecurityContext}
102114 *
103- * @return securityContext
115+ * @return The {@link SecurityContext} instance
104116 */
105117 private SecurityContext securityContext () {
106- return SecurityContext .builder ().securityReferences (this .defaultAuth ()).forPaths (PathSelectors .regex (swaggerProperties .getAuthorization ().getAuthRegex ())).build ();
118+ return SecurityContext .builder ()
119+ .securityReferences (this .defaultAuth ())
120+ .forPaths (PathSelectors .regex (swaggerProperties .getAuthorization ().getAuthRegex ()))
121+ .build ();
107122 }
108123
109124 /**
110- * 配置默认权限
125+ * Configure {@link SecurityReference}
126+ * <p>
127+ * The configured reference will be displayed on the dialog
111128 *
112- * @return SecurityReference arrays
129+ * @return The {@link SecurityReference} list
113130 */
114131 private List <SecurityReference > defaultAuth () {
115- AuthorizationScope authorizationScope = new AuthorizationScope ("global" , "accessEverything" );
116- AuthorizationScope [] authorizationScopes = new AuthorizationScope []{authorizationScope };
117- return Collections .singletonList (SecurityReference .builder ().reference (swaggerProperties .getAuthorization ().getName ()).scopes (authorizationScopes ).build ());
132+ AuthorizationScope authorizationScope = new AuthorizationScope (AUTHORIZATION_SCOPE , AUTHORIZATION_SCOPE_DESCRIPTION );
133+ AuthorizationScope [] authorizationScopes = Arrays .asList (authorizationScope ).stream ().toArray (AuthorizationScope []::new );
134+ SecurityReference securityReference =
135+ SecurityReference .builder ()
136+ .reference (swaggerProperties .getAuthorization ().getName ())
137+ .scopes (authorizationScopes )
138+ .build ();
139+ return Collections .singletonList (securityReference );
140+ }
141+
142+ /**
143+ * Convert {@link SwaggerProperties.Contact} to {@link Contact}
144+ *
145+ * @param contact The {@link SwaggerProperties.Contact} instance
146+ * @return {@link Contact} instance
147+ */
148+ private Contact convertContact (SwaggerProperties .Contact contact ) {
149+ return new Contact (contact .getName (), contact .getWebsite (), contact .getEmail ());
118150 }
119151}
0 commit comments