mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-09 04:59:21 +08:00
feat: 新增跨域自动配置
This commit is contained in:
@@ -14,4 +14,56 @@
|
|||||||
|
|
||||||
<name>${project.artifactId}</name>
|
<name>${project.artifactId}</name>
|
||||||
<description>ContiNew Starter 核心模块</description>
|
<description>ContiNew Starter 核心模块</description>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- Spring Boot Web(提供 Spring MVC Web 开发能力,默认内置 Tomcat 服务器) -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
<!-- 移除内置 Tomcat 服务器 -->
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-tomcat</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Undertow 服务器(采用 Java 开发的灵活的高性能 Web 服务器,提供包括阻塞和基于 NIO 的非堵塞机制) -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-undertow</artifactId>
|
||||||
|
<!-- 移除 websocket 依赖,后续使用 websocket 可考虑由 Netty 提供。另可解决日志警告:UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used -->
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>io.undertow</groupId>
|
||||||
|
<artifactId>undertow-websockets-jsr</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Spring Boot Starter(自动配置相关依赖) -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Hutool(小而全的 Java 工具类库,通过静态方法封装,降低相关 API 的学习成本,提高工作效率,使 Java 拥有函数式语言般的优雅,让 Java 语言也可以“甜甜的”) -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.hutool</groupId>
|
||||||
|
<artifactId>hutool-all</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Lombok(在 Java 开发过程中用注解的方式,简化了 JavaBean 的编写,避免了冗余和样板式代码,让编写的类更加简洁) -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<optional>true</optional> <!-- 表示依赖不会被传递 -->
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
</project>
|
</project>
|
@@ -0,0 +1,71 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||||
|
* <p>
|
||||||
|
* 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
|
||||||
|
* <p>
|
||||||
|
* http://www.gnu.org/licenses/lgpl.html
|
||||||
|
* <p>
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,59 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||||
|
* <p>
|
||||||
|
* 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
|
||||||
|
* <p>
|
||||||
|
* http://www.gnu.org/licenses/lgpl.html
|
||||||
|
* <p>
|
||||||
|
* 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<String> allowedOrigins = new ArrayList<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 允许跨域的请求方式
|
||||||
|
*/
|
||||||
|
private List<String> allowedMethods = new ArrayList<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 允许跨域的请求头
|
||||||
|
*/
|
||||||
|
private List<String> allowedHeaders = new ArrayList<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 允许跨域的响应头
|
||||||
|
*/
|
||||||
|
private List<String> exposedHeaders = new ArrayList<>();
|
||||||
|
}
|
@@ -0,0 +1,61 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||||
|
* <p>
|
||||||
|
* 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
|
||||||
|
* <p>
|
||||||
|
* http://www.gnu.org/licenses/lgpl.html
|
||||||
|
* <p>
|
||||||
|
* 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 = ",";
|
||||||
|
}
|
@@ -0,0 +1 @@
|
|||||||
|
top.charles7c.continew.starter.core.autoconfigure.cors.CorsAutoConfiguration
|
@@ -11,13 +11,28 @@
|
|||||||
|
|
||||||
<name>${project.artifactId}</name>
|
<name>${project.artifactId}</name>
|
||||||
<description>ContiNew Starter 依赖模块</description>
|
<description>ContiNew Starter 依赖模块</description>
|
||||||
|
<url>https://github.com/Charles7c/continew-starter</url>
|
||||||
|
<licenses>
|
||||||
|
<license>
|
||||||
|
<name>GNU LESSER GENERAL PUBLIC LICENSE</name>
|
||||||
|
<url>http://www.gnu.org/licenses/lgpl.html</url>
|
||||||
|
</license>
|
||||||
|
</licenses>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<revision>1.0.0-SNAPSHOT</revision>
|
<revision>1.0.0-SNAPSHOT</revision>
|
||||||
|
<hutool.version>5.8.23</hutool.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<!-- Hutool(小而全的 Java 工具类库,通过静态方法封装,降低相关 API 的学习成本,提高工作效率,使 Java 拥有函数式语言般的优雅,让 Java 语言也可以“甜甜的”) -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.hutool</groupId>
|
||||||
|
<artifactId>hutool-all</artifactId>
|
||||||
|
<version>${hutool.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- 核心模块 -->
|
<!-- 核心模块 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>top.charles7c.continew</groupId>
|
<groupId>top.charles7c.continew</groupId>
|
||||||
@@ -26,4 +41,28 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</dependencyManagement>
|
</dependencyManagement>
|
||||||
|
|
||||||
|
<developers>
|
||||||
|
<developer>
|
||||||
|
<id>charles7c</id>
|
||||||
|
<name>Charles7c</name>
|
||||||
|
<email>charles7c@126.com</email>
|
||||||
|
<roles>
|
||||||
|
<role>Creator</role>
|
||||||
|
<role>Java Development Engineer</role>
|
||||||
|
</roles>
|
||||||
|
<timezone>+8</timezone>
|
||||||
|
<url>https://github.com/Charles7c</url>
|
||||||
|
</developer>
|
||||||
|
<developer>
|
||||||
|
<id>bull-bcls</id>
|
||||||
|
<name>Bull-BCLS</name>
|
||||||
|
<email>Tomcat416@163.com</email>
|
||||||
|
<roles>
|
||||||
|
<role>Java Development Engineer</role>
|
||||||
|
</roles>
|
||||||
|
<timezone>+8</timezone>
|
||||||
|
<url>https://github.com/Bull-BCLS</url>
|
||||||
|
</developer>
|
||||||
|
</developers>
|
||||||
</project>
|
</project>
|
14
pom.xml
14
pom.xml
@@ -45,8 +45,9 @@
|
|||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<!-- ### 基础环境相关 ### -->
|
|
||||||
<revision>1.0.0-SNAPSHOT</revision>
|
<revision>1.0.0-SNAPSHOT</revision>
|
||||||
|
<spring-boot.version>3.1.5</spring-boot.version>
|
||||||
|
|
||||||
<spotless.version>2.30.0</spotless.version>
|
<spotless.version>2.30.0</spotless.version>
|
||||||
<maven.compiler.source>17</maven.compiler.source>
|
<maven.compiler.source>17</maven.compiler.source>
|
||||||
<maven.compiler.target>17</maven.compiler.target>
|
<maven.compiler.target>17</maven.compiler.target>
|
||||||
@@ -55,7 +56,16 @@
|
|||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<!-- ContiNew Starter 依赖模块 -->
|
<!-- Spring 依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-dependencies</artifactId>
|
||||||
|
<version>${spring-boot.version}</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- ContiNew Starter 依赖 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>top.charles7c.continew</groupId>
|
<groupId>top.charles7c.continew</groupId>
|
||||||
<artifactId>continew-starter-dependencies</artifactId>
|
<artifactId>continew-starter-dependencies</artifactId>
|
||||||
|
Reference in New Issue
Block a user