603 Star 3.9K Fork 855

GVP萧明 / knife4j

 / 详情

knife4j升级到3.0未显示authorize输入栏

Done
Opened this issue  
2020-10-28 10:49

一、项目环境


<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <version>3.0</version>
</dependency>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.2.10.RELEASE</version>
</dependency>

二、配置类

@Slf4j
@Configuration
@EnableOpenApi
@EnableKnife4j
@EnableConfigurationProperties({CustomerSwaggerProperties.class})
public class SwaggerAutoConfiguration {

	@Bean
	@ConditionalOnProperty(name = "xxx.xxx.swagger.enabled",havingValue = "true")
	public SecurityBasicAuthFilter securityBasicAuthFilter(CustomerSwaggerProperties customerSwaggerProperties){
		return new SecurityBasicAuthFilter(
				customerSwaggerProperties.getSecurity().isEnabled(),
				customerSwaggerProperties.getSecurity().getUsername(),
				customerSwaggerProperties.getSecurity().getPassword());
	}

	@Bean
	@ConditionalOnMissingBean
	public Docket createRestApi(CustomerSwaggerProperties properties) {
		return new Docket(DocumentationType.OAS_30)
				.enable(properties.isEnabled())
				.apiInfo(apiInfo(properties))
				.select()
				.apis(RequestHandlerSelectors.basePackage(properties.getBasePackage()))
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
				.paths(PathSelectors.any())
				.build()
				.securitySchemes(security(properties.getApiInfo().getAuthorizationHeaderName()))
				.securityContexts(securityContexts(properties.getApiInfo().getAuthorizationHeaderName()));
	}

	private List<SecurityContext> securityContexts(String authorizationHeaderName) {
		return Arrays.asList(
				SecurityContext.builder()
						.securityReferences(defaultAuth(authorizationHeaderName))
						.forPaths(PathSelectors.regex("^(?!auth).*$"))
						.build()
		);

	}

	private List<SecurityReference> defaultAuth(String authorizationHeaderName) {
		AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
		AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
		authorizationScopes[0] = authorizationScope;
		return Arrays.asList(
				new SecurityReference(authorizationHeaderName, authorizationScopes));
	}


	private List<SecurityScheme> security(String authorizationHeaderName) {
		return Arrays.asList(
				new ApiKey(authorizationHeaderName, authorizationHeaderName, "header")
		);
	}

	private ApiInfo apiInfo(CustomerSwaggerProperties properties) {
		return new ApiInfoBuilder()
				.title(properties.getApiInfo().getTitle())
				.description(properties.getApiInfo().getDescription())
				//.termsOfServiceUrl("http://terms-of-services.url")
				.version(properties.getApiInfo().getVersion())
				.build();
	}
}

三、异常页面

3.1 原生swagger-3.0正常展示

输入图片说明

3.2 knife4j-3.0.0无法正常展示

输入图片说明

Comments (1)

人中泰迪 created任务
人中泰迪 set related repository to 萧明/knife4j
人中泰迪 changed description
萧明 changed issue state from 待办的 to 进行中
萧明 added
 
enhancement
label
萧明 added
 
bug
label
展开全部操作日志
萧明 changed issue state from 进行中 to 已完成

Sign in to comment

状态
Assignees
Labels
Milestones
Pull Requests
关联的 Pull Requests 被合并后可能会关闭此 issue
Branches
Planed to start   -   Planed to end
-
Top level
Priority
参与者(2)
118100 xiaoym 1578918321
Java
1
https://gitee.com/xiaoym/knife4j.git
git@gitee.com:xiaoym/knife4j.git
xiaoym
knife4j
knife4j

Search