From 2c1b8054e7f7307052388e3e6ca4111a4bdb1244 Mon Sep 17 00:00:00 2001
From: xiongxiaoyang <773861846@qq.com>
Date: Tue, 17 May 2022 08:47:28 +0800
Subject: [PATCH] =?UTF-8?q?chore:=20=E9=85=8D=E7=BD=AE=E8=B0=83=E6=95=B4?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../xxyopen/novel/core/config/CorsConfig.java | 11 ++++++++--
 .../novel/core/config/CorsProperties.java     | 22 +++++++++++++++++++
 .../core/constant/SystemConfigConsts.java     |  5 -----
 .../xxyopen/novel/core/util/JwtUtils.java     |  4 ++--
 src/main/resources/application.yml            | 16 +++++++++++---
 5 files changed, 46 insertions(+), 12 deletions(-)
 create mode 100644 src/main/java/io/github/xxyopen/novel/core/config/CorsProperties.java

diff --git a/src/main/java/io/github/xxyopen/novel/core/config/CorsConfig.java b/src/main/java/io/github/xxyopen/novel/core/config/CorsConfig.java
index 7d53faf..b3d22f2 100644
--- a/src/main/java/io/github/xxyopen/novel/core/config/CorsConfig.java
+++ b/src/main/java/io/github/xxyopen/novel/core/config/CorsConfig.java
@@ -1,6 +1,7 @@
 package io.github.xxyopen.novel.core.config;
 
-import io.github.xxyopen.novel.core.constant.SystemConfigConsts;
+import lombok.RequiredArgsConstructor;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.web.cors.CorsConfiguration;
@@ -14,13 +15,19 @@ import org.springframework.web.filter.CorsFilter;
  * @date 2022/5/13
  */
 @Configuration
+@EnableConfigurationProperties(CorsProperties.class)
+@RequiredArgsConstructor
 public class CorsConfig {
 
+    private final CorsProperties corsProperties;
+
     @Bean
     public CorsFilter corsFilter() {
         CorsConfiguration config = new CorsConfiguration();
         // 允许的域,不要写*,否则cookie就无法使用了
-        config.addAllowedOrigin(SystemConfigConsts.NOVEL_FRONT_WEB_ORIGIN);
+        for (String allowOrigin : corsProperties.getAllowOrigins()) {
+            config.addAllowedOrigin(allowOrigin);
+        }
         // 允许的头信息
         config.addAllowedHeader("*");
         // 允许的请求方式
diff --git a/src/main/java/io/github/xxyopen/novel/core/config/CorsProperties.java b/src/main/java/io/github/xxyopen/novel/core/config/CorsProperties.java
new file mode 100644
index 0000000..51f5cfe
--- /dev/null
+++ b/src/main/java/io/github/xxyopen/novel/core/config/CorsProperties.java
@@ -0,0 +1,22 @@
+package io.github.xxyopen.novel.core.config;
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+
+import java.util.List;
+
+/**
+ * 跨域配置属性
+ *
+ * @author xiongxiaoyang
+ * @date 2022/5/17
+ */
+@ConfigurationProperties(prefix = "novel.cors")
+@Data
+public class CorsProperties {
+
+    /**
+     * 允许跨域的域名
+     * */
+    private List<String> allowOrigins;
+}
diff --git a/src/main/java/io/github/xxyopen/novel/core/constant/SystemConfigConsts.java b/src/main/java/io/github/xxyopen/novel/core/constant/SystemConfigConsts.java
index 77fafed..0024141 100644
--- a/src/main/java/io/github/xxyopen/novel/core/constant/SystemConfigConsts.java
+++ b/src/main/java/io/github/xxyopen/novel/core/constant/SystemConfigConsts.java
@@ -27,9 +27,4 @@ public class SystemConfigConsts {
      * */
     public static final String NOVEL_ADMIN_KEY = "admin";
 
-    /**
-     * 小说前台门户系统域
-     * */
-    public static final String NOVEL_FRONT_WEB_ORIGIN = "http://localhost:1024";
-
 }
diff --git a/src/main/java/io/github/xxyopen/novel/core/util/JwtUtils.java b/src/main/java/io/github/xxyopen/novel/core/util/JwtUtils.java
index 5bb1bdd..723d889 100644
--- a/src/main/java/io/github/xxyopen/novel/core/util/JwtUtils.java
+++ b/src/main/java/io/github/xxyopen/novel/core/util/JwtUtils.java
@@ -19,7 +19,7 @@ import java.util.Objects;
  * @author xiongxiaoyang
  * @date 2022/5/17
  */
-@ConditionalOnProperty("jwt.secret")
+@ConditionalOnProperty("novel.jwt.secret")
 @Component
 @Slf4j
 public class JwtUtils {
@@ -27,7 +27,7 @@ public class JwtUtils {
     /**
      * 注入JWT加密密钥
      */
-    @Value("${jwt.secret}")
+    @Value("${novel.jwt.secret}")
     private String secret;
 
     /**
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index e8452a7..1f66e26 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -42,6 +42,16 @@ spring:
   config:
     activate:
       on-profile: dev
-# JWT密钥
-jwt:
-  secret: E66559580A1ADF48CDD928516062F12E
\ No newline at end of file
+
+# 项目配置
+novel:
+  # 跨域配置
+  cors:
+    # 允许跨域的域名
+    allow-origins:
+      - http://localhost:1024
+      - http://localhost:8080
+  # JWT密钥
+  jwt:
+    secret: E66559580A1ADF48CDD928516062F12E
+