mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-20 06:34:55 +00:00
[feature][project] Enable authentication support for Swagger (#1791)
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
package com.tencent.supersonic.config;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.tencent.supersonic.auth.api.authentication.config.AuthenticationConfig;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
@@ -13,63 +12,95 @@ import springfox.documentation.oas.annotations.EnableOpenApi;
|
||||
import springfox.documentation.service.ApiInfo;
|
||||
import springfox.documentation.service.ApiKey;
|
||||
import springfox.documentation.service.Contact;
|
||||
import springfox.documentation.service.SecurityReference;
|
||||
import springfox.documentation.spi.DocumentationType;
|
||||
import springfox.documentation.spi.service.contexts.SecurityContext;
|
||||
import springfox.documentation.spring.web.plugins.Docket;
|
||||
import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@Configuration
|
||||
@EnableSwagger2
|
||||
@EnableOpenApi
|
||||
public class SwaggerConfiguration {
|
||||
|
||||
/** 标题 */
|
||||
/**
|
||||
* 标题
|
||||
*/
|
||||
@Value("${swagger.title}")
|
||||
private String title;
|
||||
|
||||
/** 基本包 */
|
||||
/**
|
||||
* 基本包
|
||||
*/
|
||||
@Value("${swagger.base.package}")
|
||||
private String basePackage;
|
||||
|
||||
/** 描述 */
|
||||
/**
|
||||
* 描述
|
||||
*/
|
||||
@Value("${swagger.description}")
|
||||
private String description;
|
||||
|
||||
/** URL */
|
||||
/**
|
||||
* URL
|
||||
*/
|
||||
@Value("${swagger.url}")
|
||||
private String url;
|
||||
|
||||
/** 作者 */
|
||||
/**
|
||||
* 作者
|
||||
*/
|
||||
@Value("${swagger.contact.name}")
|
||||
private String contactName;
|
||||
|
||||
/** 作者网址 */
|
||||
/**
|
||||
* 作者网址
|
||||
*/
|
||||
@Value("${swagger.contact.url}")
|
||||
private String contactUrl;
|
||||
|
||||
/** 作者邮箱 */
|
||||
/**
|
||||
* 作者邮箱
|
||||
*/
|
||||
@Value("${swagger.contact.email}")
|
||||
private String contactEmail;
|
||||
|
||||
/** 版本 */
|
||||
/**
|
||||
* 版本
|
||||
*/
|
||||
@Value("${swagger.version}")
|
||||
private String version;
|
||||
|
||||
@Autowired
|
||||
private AuthenticationConfig authenticationConfig;
|
||||
|
||||
@Bean
|
||||
public Docket createRestApi() {
|
||||
|
||||
return new Docket(DocumentationType.OAS_30).apiInfo(apiInfo()).select()
|
||||
.apis(RequestHandlerSelectors.basePackage(basePackage)).paths(PathSelectors.any())
|
||||
.build().securitySchemes(Lists.newArrayList(apiKey()));
|
||||
}
|
||||
|
||||
private ApiKey apiKey() {
|
||||
return new ApiKey(authenticationConfig.getTokenHttpHeaderKey(),
|
||||
authenticationConfig.getTokenHttpHeaderKey(), "header");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Docket createRestApi() {
|
||||
return new Docket(DocumentationType.OAS_30).apiInfo(apiInfo()).select()
|
||||
.apis(RequestHandlerSelectors.basePackage(basePackage)).paths(PathSelectors.any())
|
||||
.build().securitySchemes(Collections.singletonList(apiKey()))
|
||||
.securityContexts(Collections.singletonList(securityContext()));
|
||||
}
|
||||
|
||||
private SecurityContext securityContext() {
|
||||
return SecurityContext.builder().securityReferences(defaultAuth()).build();
|
||||
}
|
||||
|
||||
private List<SecurityReference> defaultAuth() {
|
||||
SecurityReference securityReference =
|
||||
new SecurityReference(authenticationConfig.getTokenHttpHeaderKey(),
|
||||
new springfox.documentation.service.AuthorizationScope[0]);
|
||||
return Collections.singletonList(securityReference);
|
||||
}
|
||||
|
||||
private ApiInfo apiInfo() {
|
||||
return new ApiInfoBuilder().title(title).description(description).termsOfServiceUrl(url)
|
||||
.contact(new Contact(contactName, contactUrl, contactEmail)).version(version)
|
||||
|
||||
Reference in New Issue
Block a user