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 @@
+ * 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
+ * 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 @@