-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
31537c9
commit ca7f919
Showing
34 changed files
with
1,932 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
47 changes: 47 additions & 0 deletions
47
...t-starter/src/main/java/com/github/xiaoymin/knife4j/spring/annotations/EnableKnife4j.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
157 changes: 157 additions & 0 deletions
157
.../main/java/com/github/xiaoymin/knife4j/spring/configuration/Knife4jAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
Oops, something went wrong.