From 3796790db408f7644531e09c56f38ed9a7d47ccb Mon Sep 17 00:00:00 2001 From: Charles7c Date: Sat, 18 Nov 2023 19:59:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E8=B7=A8=E5=9F=9F?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- continew-starter-core/pom.xml | 52 ++++++++++++++ .../cors/CorsAutoConfiguration.java | 71 +++++++++++++++++++ .../autoconfigure/cors/CorsProperties.java | 59 +++++++++++++++ .../starter/core/constant/StringConsts.java | 61 ++++++++++++++++ ...ot.autoconfigure.AutoConfiguration.imports | 1 + continew-starter-dependencies/pom.xml | 39 ++++++++++ pom.xml | 14 +++- 7 files changed, 295 insertions(+), 2 deletions(-) create mode 100644 continew-starter-core/src/main/java/top/charles7c/continew/starter/core/autoconfigure/cors/CorsAutoConfiguration.java create mode 100644 continew-starter-core/src/main/java/top/charles7c/continew/starter/core/autoconfigure/cors/CorsProperties.java create mode 100644 continew-starter-core/src/main/java/top/charles7c/continew/starter/core/constant/StringConsts.java create mode 100644 continew-starter-core/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports diff --git a/continew-starter-core/pom.xml b/continew-starter-core/pom.xml index a96e49c1..8ee51493 100644 --- a/continew-starter-core/pom.xml +++ b/continew-starter-core/pom.xml @@ -14,4 +14,56 @@ ${project.artifactId} ContiNew Starter 核心模块 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-tomcat + + + + + + + org.springframework.boot + spring-boot-starter-undertow + + + + io.undertow + undertow-websockets-jsr + + + + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-configuration-processor + true + + + + + cn.hutool + hutool-all + + + + + org.projectlombok + lombok + true + + \ No newline at end of file diff --git a/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/autoconfigure/cors/CorsAutoConfiguration.java b/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/autoconfigure/cors/CorsAutoConfiguration.java new file mode 100644 index 00000000..7de7cf4e --- /dev/null +++ b/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/autoconfigure/cors/CorsAutoConfiguration.java @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * 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 top.charles7c.continew.starter.core.autoconfigure.cors; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Lazy; +import org.springframework.web.cors.CorsConfiguration; +import org.springframework.web.cors.UrlBasedCorsConfigurationSource; +import org.springframework.web.filter.CorsFilter; +import top.charles7c.continew.starter.core.constant.StringConsts; + +/** + * 跨域自动配置 + * + * @author Charles7c + * @since 1.0.0 + */ +@Lazy +@Configuration(proxyBeanMethods = false) +@ConditionalOnWebApplication +@ConditionalOnProperty(prefix = "cors", name = "enabled", havingValue = "true") +@EnableConfigurationProperties(CorsProperties.class) +public class CorsAutoConfiguration { + + @Bean + @ConditionalOnMissingBean + public CorsFilter corsFilter(CorsProperties properties) { + CorsConfiguration config = new CorsConfiguration(); + // 设置跨域允许时间 + config.setMaxAge(1800L); + + // 配置允许跨域的域名 + if (properties.getAllowedOrigins().contains(StringConsts.ASTERISK)) { + config.addAllowedOriginPattern(StringConsts.ASTERISK); + } else { + // 配置为 true 后则必须配置允许跨域的域名,且不允许配置为 * + config.setAllowCredentials(true); + properties.getAllowedOrigins().forEach(config::addAllowedOrigin); + } + // 配置允许跨域的请求方式 + properties.getAllowedMethods().forEach(config::addAllowedMethod); + // 配置允许跨域的请求头 + properties.getAllowedHeaders().forEach(config::addAllowedHeader); + // 配置允许跨域的响应头 + properties.getExposedHeaders().forEach(config::addExposedHeader); + + // 添加映射路径,拦截一切请求 + UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); + source.registerCorsConfiguration("/**", config); + return new CorsFilter(source); + } +} diff --git a/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/autoconfigure/cors/CorsProperties.java b/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/autoconfigure/cors/CorsProperties.java new file mode 100644 index 00000000..53373a47 --- /dev/null +++ b/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/autoconfigure/cors/CorsProperties.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * 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 top.charles7c.continew.starter.core.autoconfigure.cors; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; + +import java.util.ArrayList; +import java.util.List; + +/** + * 跨域配置属性 + * + * @author Charles7c + * @since 1.0.0 + */ +@Data +@ConfigurationProperties(prefix = "cors") +public class CorsProperties { + + /** + * 是否启用 + */ + private boolean enabled = false; + + /** + * 允许跨域的域名 + */ + private List allowedOrigins = new ArrayList<>(); + + /** + * 允许跨域的请求方式 + */ + private List allowedMethods = new ArrayList<>(); + + /** + * 允许跨域的请求头 + */ + private List allowedHeaders = new ArrayList<>(); + + /** + * 允许跨域的响应头 + */ + private List exposedHeaders = new ArrayList<>(); +} diff --git a/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/constant/StringConsts.java b/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/constant/StringConsts.java new file mode 100644 index 00000000..87240027 --- /dev/null +++ b/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/constant/StringConsts.java @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * 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 top.charles7c.continew.starter.core.constant; + +import cn.hutool.core.text.StrPool; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +/** + * 字符串相关常量 + * + * @author Charles7c + * @since 1.0.0 + */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class StringConsts implements StrPool { + + /** + * 空字符串 + */ + public static final String EMPTY = ""; + + /** + * 空格 + */ + public static final String SPACE = " "; + + /** + * 分号 + */ + public static final String SEMICOLON = ";"; + + /** + * 星号 + */ + public static final String ASTERISK = "*"; + + /** + * 问号 + */ + public static final String QUESTION_MARK = "?"; + + /** + * 中文逗号 + */ + public static final String CHINESE_COMMA = ","; +} diff --git a/continew-starter-core/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/continew-starter-core/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 00000000..65bae9d0 --- /dev/null +++ b/continew-starter-core/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +top.charles7c.continew.starter.core.autoconfigure.cors.CorsAutoConfiguration diff --git a/continew-starter-dependencies/pom.xml b/continew-starter-dependencies/pom.xml index 23bdfeb7..7031b586 100644 --- a/continew-starter-dependencies/pom.xml +++ b/continew-starter-dependencies/pom.xml @@ -11,13 +11,28 @@ ${project.artifactId} ContiNew Starter 依赖模块 + https://github.com/Charles7c/continew-starter + + + GNU LESSER GENERAL PUBLIC LICENSE + http://www.gnu.org/licenses/lgpl.html + + 1.0.0-SNAPSHOT + 5.8.23 + + + cn.hutool + hutool-all + ${hutool.version} + + top.charles7c.continew @@ -26,4 +41,28 @@ + + + + charles7c + Charles7c + charles7c@126.com + + Creator + Java Development Engineer + + +8 + https://github.com/Charles7c + + + bull-bcls + Bull-BCLS + Tomcat416@163.com + + Java Development Engineer + + +8 + https://github.com/Bull-BCLS + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 6dec0f96..55002261 100644 --- a/pom.xml +++ b/pom.xml @@ -45,8 +45,9 @@ - 1.0.0-SNAPSHOT + 3.1.5 + 2.30.0 17 17 @@ -55,7 +56,16 @@ - + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + top.charles7c.continew continew-starter-dependencies