Skip to content

Commit

Permalink
将knife4j拆出来重新依赖xiaoymin/knife4j#865
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangchaoxu committed Dec 25, 2024
1 parent 31537c9 commit ca7f919
Show file tree
Hide file tree
Showing 34 changed files with 1,932 additions and 17 deletions.
2 changes: 1 addition & 1 deletion cms/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>onex-boot</artifactId>
<groupId>com.nb6868.onex</groupId>
<version>4.2.17</version>
<version>4.3.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cms</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions coder/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>onex-boot</artifactId>
<groupId>com.nb6868.onex</groupId>
<version>4.2.17</version>
<version>4.3.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>coder</artifactId>
Expand All @@ -17,7 +17,7 @@
<!-- version -->
<druid.version>1.2.23</druid.version>
<commons-configuration.version>1.10</commons-configuration.version>
<hutool.version>5.8.33</hutool.version>
<hutool.version>5.8.34</hutool.version>
<velocity.version>1.7</velocity.version>
<commons-collections.version>3.2.2</commons-collections.version>
<screw.version>1.0.5</screw.version>
Expand Down
13 changes: 9 additions & 4 deletions common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>onex-boot</artifactId>
<groupId>com.nb6868.onex</groupId>
<version>4.2.17</version>
<version>4.3.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>common</artifactId>
Expand All @@ -29,7 +29,7 @@
<weixin.version>4.6.0</weixin.version>
<aliyun-oss.version>3.18.1</aliyun-oss.version>
<huaweicloud-obs.version>3.24.9</huaweicloud-obs.version>
<aws-s3.version>2.29.39</aws-s3.version>
<aws-s3.version>2.29.40</aws-s3.version>
<poi.version>5.3.0</poi.version>
<poi-tl.version>1.12.2</poi-tl.version>
<easyexcel.version>4.0.3</easyexcel.version>
Expand Down Expand Up @@ -151,13 +151,18 @@
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
<!-- knife4j+ -->
<!-- knife4j+
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
<version>${knife4j.version}</version>
</dependency>
</dependency>-->
<!-- poi -->
<dependency>
<groupId>com.nb6868.onex</groupId>
<artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
<version>${onex.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions common/src/main/resources/application-common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,14 @@ onex:

## swagger
knife4j:
# 增强
enable: true
# 标题
title: 'OneX-API'
# 描述
description: 'API document'
# version
version: '1.0.0'
# 增强
enable: true
setting:
# 分组
enable-group: true
Expand Down
2 changes: 1 addition & 1 deletion job/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>onex-boot</artifactId>
<groupId>com.nb6868.onex</groupId>
<version>4.2.17</version>
<version>4.3.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>job</artifactId>
Expand Down
69 changes: 69 additions & 0 deletions knife4j-openapi3-jakarta-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>onex-boot</artifactId>
<groupId>com.nb6868.onex</groupId>
<version>4.3.0</version>
</parent>

<artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>

<properties>
<knife4j-java.version>17</knife4j-java.version>
<knife4j-spring3.version>6.0.6</knife4j-spring3.version>
<knife4j-spring-boot3.version>3.4.1</knife4j-spring-boot3.version>

<knife4j-javassist.version>3.29.2-GA</knife4j-javassist.version>
<knife4j.version>4.5.0</knife4j.version>
<maven-compiler-plugin.version>3.10.1</maven-compiler-plugin.version>
<knife4j-skipTests>true</knife4j-skipTests>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>${knife4j-spring-boot3.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-core</artifactId>
<version>${knife4j.version}</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi3-ui</artifactId>
<version>${knife4j.version}</version>
</dependency>

<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<scope>provided</scope>
<version>6.0.0</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${knife4j-java.version}</source>
<target>${knife4j-java.version}</target>
<encoding>${knife4j-project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright © 2017-2023 Knife4j([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 com.github.xiaoymin.knife4j.spring.annotations;

import com.github.xiaoymin.knife4j.spring.configuration.Knife4jAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Import;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/***
* Enable Knife4j enhanced annotation and use @EnableSwagger2 annotation together.
* <p>
* inlude:
* <ul>
* <li>Interface sorting </li>
* <li>Interface document download (word)</li>
* </ul>
* @author xiaoymin
* @since 2.0.0
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Import({Knife4jAutoConfiguration.class})
@ConditionalOnWebApplication
public @interface EnableKnife4j {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
* Copyright © 2017-2023 Knife4j([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 com.github.xiaoymin.knife4j.spring.configuration;

import com.github.xiaoymin.knife4j.core.conf.GlobalConstants;
import com.github.xiaoymin.knife4j.extend.filter.basic.AbstractSecurityFilter;
import com.github.xiaoymin.knife4j.extend.filter.basic.JakartaServletSecurityBasicAuthFilter;
import com.github.xiaoymin.knife4j.spring.extension.Knife4jJakartaOperationCustomizer;
import com.github.xiaoymin.knife4j.spring.extension.Knife4jOpenApiCustomizer;
import com.github.xiaoymin.knife4j.spring.filter.JakartaProductionSecurityFilter;
import com.github.xiaoymin.knife4j.spring.util.EnvironmentUtils;
import jakarta.servlet.DispatcherType;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.properties.SpringDocConfigProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/***
* Knife4j 基础自动配置类
* {@code @since } 2.0.0
* @author <a href="mailto:[email protected]">[email protected]</a>
* 2019/08/28 21:08
*/
@Slf4j
@AllArgsConstructor
@Configuration
@EnableConfigurationProperties({Knife4jProperties.class, Knife4jSetting.class, Knife4jHttpBasic.class})
@ConditionalOnProperty(name = "knife4j.enable", havingValue = "true")
public class Knife4jAutoConfiguration {

private final Knife4jProperties properties;
private final Environment environment;

/**
* 增强自定义配置
* @return
*/
@Bean
@ConditionalOnMissingBean
public Knife4jOpenApiCustomizer knife4jOpenApiCustomizer(SpringDocConfigProperties docProperties) {
log.debug("Register Knife4jOpenApiCustomizer");
return new Knife4jOpenApiCustomizer(this.properties, docProperties);
}

@Bean
@ConditionalOnMissingBean
public Knife4jJakartaOperationCustomizer knife4jJakartaOperationCustomizer() {
return new Knife4jJakartaOperationCustomizer();
}
/**
* 配置Cors
*
* @since 2.0.4
*/
@Bean("knife4jCorsFilter")
@ConditionalOnMissingBean(CorsFilter.class)
@ConditionalOnProperty(name = "knife4j.cors", havingValue = "true")
public CorsFilter corsFilter() {
log.info("init CorsFilter...");
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.setAllowCredentials(true);
corsConfiguration.addAllowedOrigin("*");
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedMethod("*");
corsConfiguration.setMaxAge(10000L);
// 匹配所有API
source.registerCorsConfiguration("/**", corsConfiguration);
CorsFilter corsFilter = new CorsFilter(source);
return corsFilter;
}

/**
* Security with Basic Http
* @param knife4jProperties Basic Properties
* @return BasicAuthFilter
*/
@Bean
@ConditionalOnMissingBean(JakartaServletSecurityBasicAuthFilter.class)
@ConditionalOnExpression("${knife4j.production:false} && ${knife4j.basic.enable:true}")
public FilterRegistrationBean<JakartaServletSecurityBasicAuthFilter> securityBasicAuthFilter(Knife4jProperties knife4jProperties) {
JakartaServletSecurityBasicAuthFilter authFilter = new JakartaServletSecurityBasicAuthFilter();
if (knife4jProperties == null) {
authFilter.setEnableBasicAuth(EnvironmentUtils.resolveBool(environment, "knife4j.basic.enable", Boolean.FALSE));
authFilter.setUserName(EnvironmentUtils.resolveString(environment, "knife4j.basic.username", GlobalConstants.BASIC_DEFAULT_USERNAME));
authFilter.setPassword(EnvironmentUtils.resolveString(environment, "knife4j.basic.password", GlobalConstants.BASIC_DEFAULT_PASSWORD));
} else {
// 判断非空
if (knife4jProperties.getBasic() == null) {
authFilter.setEnableBasicAuth(Boolean.FALSE);
authFilter.setUserName(GlobalConstants.BASIC_DEFAULT_USERNAME);
authFilter.setPassword(GlobalConstants.BASIC_DEFAULT_PASSWORD);
} else {
authFilter.setEnableBasicAuth(knife4jProperties.getBasic().isEnable());
authFilter.setUserName(knife4jProperties.getBasic().getUsername());
authFilter.setPassword(knife4jProperties.getBasic().getPassword());
authFilter.addRule(knife4jProperties.getBasic().getInclude());
}
}
FilterRegistrationBean<JakartaServletSecurityBasicAuthFilter> registration = new FilterRegistrationBean<>();
registration.setDispatcherTypes(DispatcherType.REQUEST);
registration.setFilter(authFilter);
registration.setOrder(Integer.MAX_VALUE);
return registration;
}

@Bean
@ConditionalOnMissingBean(JakartaProductionSecurityFilter.class)
@ConditionalOnProperty(name = "knife4j.production", havingValue = "true")
public FilterRegistrationBean<JakartaProductionSecurityFilter> productionSecurityFilter(Environment environment) {
boolean prod = false;
JakartaProductionSecurityFilter p = null;
if (properties == null) {
if (environment != null) {
String prodStr = environment.getProperty("knife4j.production");
if (log.isDebugEnabled()) {
log.debug("swagger.production:{}", prodStr);
}
prod = Boolean.valueOf(prodStr);
}
p = new JakartaProductionSecurityFilter(prod);
} else {
p = new JakartaProductionSecurityFilter(properties.isProduction());
}
FilterRegistrationBean<JakartaProductionSecurityFilter> registration = new FilterRegistrationBean<>();
registration.setDispatcherTypes(DispatcherType.REQUEST);
registration.setFilter(p);
registration.setOrder(Integer.MAX_VALUE - 1);
return registration;
}

}
Loading

0 comments on commit ca7f919

Please sign in to comment.