Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
dj
/
YouJia
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit ae4178ca
authored
Sep 30, 2026
by
叶立庆
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
跨域配置改造
1 parent
40e3b8c9
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
189 additions
and
11 deletions
docs/nacos/youjia-beta.yaml
docs/nacos/youjia-release.yaml
docs/nacos/youjia-test.yaml
youjia-framework/src/main/java/com/youjia/framework/config/CorsProperties.java
youjia-framework/src/main/java/com/youjia/framework/config/WebMvcConfig.java
youjia-server/src/main/resources/application-dev.yml
docs/nacos/youjia-beta.yaml
View file @
ae4178c
...
@@ -69,6 +69,30 @@ knife4j:
...
@@ -69,6 +69,30 @@ knife4j:
youjia
:
youjia
:
log
:
log
:
home
:
/opt/youjia/logs
home
:
/opt/youjia/logs
# ------------------------------------------------------------
# 跨域(CORS):前缀 youjia.cors,详见 WebMvcConfig / CorsProperties
# 生产建议把 allowed-origins 收紧为前端真实域名白名单
# (保留 ['*'] 即等价于改造前「任意源 + 允许凭证」的全开放行为)
# ------------------------------------------------------------
cors
:
enabled
:
true
path-patterns
:
-
/**
allowed-origins
:
-
'
*'
allowed-methods
:
-
GET
-
POST
-
OPTIONS
allowed-headers
:
-
'
*'
exposed-headers
:
-
X-Sign
-
X-Sign-Algorithm
-
X-Sign-Timestamp
-
X-Sign-Nonce
allow-credentials
:
true
max-age
:
3600
api-sign
:
api-sign
:
enabled
:
true
enabled
:
true
mode
:
STRICT
mode
:
STRICT
...
...
docs/nacos/youjia-release.yaml
View file @
ae4178c
...
@@ -70,6 +70,30 @@ knife4j:
...
@@ -70,6 +70,30 @@ knife4j:
youjia
:
youjia
:
log
:
log
:
home
:
/opt/youjia/logs
home
:
/opt/youjia/logs
# ------------------------------------------------------------
# 跨域(CORS):前缀 youjia.cors,详见 WebMvcConfig / CorsProperties
# 生产建议把 allowed-origins 收紧为前端真实域名白名单
# (保留 ['*'] 即等价于改造前「任意源 + 允许凭证」的全开放行为)
# ------------------------------------------------------------
cors
:
enabled
:
true
path-patterns
:
-
/**
allowed-origins
:
-
'
*'
allowed-methods
:
-
GET
-
POST
-
OPTIONS
allowed-headers
:
-
'
*'
exposed-headers
:
-
X-Sign
-
X-Sign-Algorithm
-
X-Sign-Timestamp
-
X-Sign-Nonce
allow-credentials
:
true
max-age
:
3600
api-sign
:
api-sign
:
enabled
:
true
enabled
:
true
mode
:
STRICT
mode
:
STRICT
...
...
docs/nacos/youjia-test.yaml
View file @
ae4178c
...
@@ -72,6 +72,30 @@ youjia:
...
@@ -72,6 +72,30 @@ youjia:
log
:
log
:
# TODO: 按需调整测试环境日志落盘目录(容器部署建议挂到持久化卷)
# TODO: 按需调整测试环境日志落盘目录(容器部署建议挂到持久化卷)
home
:
/opt/youjia/logs
home
:
/opt/youjia/logs
# ------------------------------------------------------------
# 跨域(CORS):前缀 youjia.cors,详见 WebMvcConfig / CorsProperties
# 生产建议把 allowed-origins 收紧为前端真实域名白名单
# (保留 ['*'] 即等价于改造前「任意源 + 允许凭证」的全开放行为)
# ------------------------------------------------------------
cors
:
enabled
:
true
path-patterns
:
-
/**
allowed-origins
:
-
'
*'
allowed-methods
:
-
GET
-
POST
-
OPTIONS
allowed-headers
:
-
'
*'
exposed-headers
:
-
X-Sign
-
X-Sign-Algorithm
-
X-Sign-Timestamp
-
X-Sign-Nonce
allow-credentials
:
true
max-age
:
3600
api-sign
:
api-sign
:
enabled
:
true
enabled
:
true
# 测试环境前端联调阶段可用 LENIENT;联调完成后改为 STRICT
# 测试环境前端联调阶段可用 LENIENT;联调完成后改为 STRICT
...
...
youjia-framework/src/main/java/com/youjia/framework/config/CorsProperties.java
0 → 100644
View file @
ae4178c
package
com
.
youjia
.
framework
.
config
;
import
lombok.Data
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
java.util.Arrays
;
import
java.util.List
;
/**
* 跨域(CORS)配置(前缀 {@code youjia.cors})。
*
* <p>全部字段都有默认值,未配置时等价于「源与请求头全开放、方法仅 GET/POST/OPTIONS、
* 暴露 X-Sign* 响应头、允许凭证」——与改造前的硬编码行为完全一致,不影响现有联调。</p>
*
* <p>生产环境建议收紧 {@code allowed-origins} 为前端真实域名白名单(如
* {@code - https://admin.example.com}),避免任意站点带凭证调用接口。</p>
*
* <p>配置既可放在 Nacos 的 {@code youjia-{env}.yaml},也可放在本地 {@code application-{env}.yml};
* Nacos 同名配置优先级更高(覆盖本地)。</p>
*
* <pre>
* youjia:
* cors:
* enabled: true
* path-patterns:
* - /**
* allowed-origins:
* - https://admin.example.com
* allowed-methods: [GET, POST, OPTIONS]
* allowed-headers: ['*']
* exposed-headers: [X-Sign, X-Sign-Algorithm, X-Sign-Timestamp, X-Sign-Nonce]
* allow-credentials: true
* max-age: 3600
* </pre>
*/
@Data
@ConfigurationProperties
(
prefix
=
"youjia.cors"
)
public
class
CorsProperties
{
/** 跨域总开关;关闭后不注册任何跨域规则(内网同源部署可关) */
private
boolean
enabled
=
true
;
/** 需要放行跨域的路径模式,默认全部 */
private
List
<
String
>
pathPatterns
=
List
.
of
(
"/**"
);
/**
* 允许的源(origin 表达式,支持 {@code *} 通配)。
* <p>此处统一走 {@code allowedOriginPatterns},因此即使 {@link #allowCredentials}=true
* 也允许写 {@code ['*']}(这是与 {@code allowedOrigins} 不同之处)。
* 生产建议改为前端真实域名列表。</p>
*/
private
List
<
String
>
allowedOrigins
=
List
.
of
(
"*"
);
/** 允许的 HTTP 方法;接口约定仅 GET/POST,OPTIONS 为预检,默认即此三项 */
private
List
<
String
>
allowedMethods
=
List
.
of
(
"GET"
,
"POST"
,
"OPTIONS"
);
/** 允许的请求头;{@code *} 表示全部 */
private
List
<
String
>
allowedHeaders
=
List
.
of
(
"*"
);
/** 暴露给浏览器的响应头;签名体系需暴露 X-Sign*,否则前端读不到响应签名 */
private
List
<
String
>
exposedHeaders
=
Arrays
.
asList
(
"X-Sign"
,
"X-Sign-Algorithm"
,
"X-Sign-Timestamp"
,
"X-Sign-Nonce"
);
/** 是否允许携带凭证(Cookie / Authorization 头) */
private
boolean
allowCredentials
=
true
;
/** 预检请求缓存时长(秒) */
private
Long
maxAge
=
3600L
;
}
youjia-framework/src/main/java/com/youjia/framework/config/WebMvcConfig.java
View file @
ae4178c
package
com
.
youjia
.
framework
.
config
;
package
com
.
youjia
.
framework
.
config
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.web.servlet.config.annotation.CorsRegistry
;
import
org.springframework.web.servlet.config.annotation.CorsRegistry
;
import
org.springframework.web.servlet.config.annotation.WebMvcConfigurer
;
import
org.springframework.web.servlet.config.annotation.WebMvcConfigurer
;
import
java.util.List
;
/**
/**
* Web MVC 配置:跨域(供前端本地联调
使用
)。
* Web MVC 配置:跨域(供前端本地联调
/ 生产按白名单放行
)。
*
*
* <p>接口层约定只使用 GET / POST 两种请求方式,跨域放行同步收敛为 GET / POST / OPTIONS。</p>
* <p>跨域参数全部来自 {@link CorsProperties}(前缀 {@code youjia.cors}),
* 可由 Nacos 的 youjia-{env}.yaml 或本地 application-{env}.yml 配置,无需改代码。</p>
*
*
* <p>
前端需要携带签名请求头(触发预检),并读取响应签名头,因此这里放开自定义请求头并
* <p>
接口层约定只使用 GET / POST,跨域同步收敛为 GET / POST / OPTIONS;
*
显式暴露响应签名头,否则浏览器侧读不到 {@code X-Sign}
。</p>
*
前端需携带签名请求头并读取响应签名头,故放开自定义请求头并暴露 X-Sign* 响应头
。</p>
*/
*/
@Configuration
@Configuration
@EnableConfigurationProperties
(
CorsProperties
.
class
)
public
class
WebMvcConfig
implements
WebMvcConfigurer
{
public
class
WebMvcConfig
implements
WebMvcConfigurer
{
private
final
CorsProperties
corsProperties
;
public
WebMvcConfig
(
CorsProperties
corsProperties
)
{
this
.
corsProperties
=
corsProperties
;
}
@Override
@Override
public
void
addCorsMappings
(
CorsRegistry
registry
)
{
public
void
addCorsMappings
(
CorsRegistry
registry
)
{
registry
.
addMapping
(
"/**"
)
if
(!
corsProperties
.
isEnabled
())
{
.
allowedOriginPatterns
(
"*"
)
return
;
.
allowedMethods
(
"GET"
,
"POST"
,
"OPTIONS"
)
}
.
allowedHeaders
(
"*"
)
String
[]
origins
=
toArray
(
corsProperties
.
getAllowedOrigins
());
.
exposedHeaders
(
"X-Sign"
,
"X-Sign-Algorithm"
,
"X-Sign-Timestamp"
,
"X-Sign-Nonce"
)
String
[]
methods
=
toArray
(
corsProperties
.
getAllowedMethods
());
.
allowCredentials
(
true
)
String
[]
headers
=
toArray
(
corsProperties
.
getAllowedHeaders
());
.
maxAge
(
3600
);
String
[]
exposed
=
toArray
(
corsProperties
.
getExposedHeaders
());
for
(
String
path
:
corsProperties
.
getPathPatterns
())
{
registry
.
addMapping
(
path
)
.
allowedOriginPatterns
(
origins
)
.
allowedMethods
(
methods
)
.
allowedHeaders
(
headers
)
.
exposedHeaders
(
exposed
)
.
allowCredentials
(
corsProperties
.
isAllowCredentials
())
.
maxAge
(
corsProperties
.
getMaxAge
());
}
}
private
static
String
[]
toArray
(
List
<
String
>
list
)
{
return
list
==
null
?
new
String
[
0
]
:
list
.
toArray
(
new
String
[
0
]);
}
}
}
}
youjia-server/src/main/resources/application-dev.yml
View file @
ae4178c
...
@@ -32,6 +32,19 @@ youjia:
...
@@ -32,6 +32,19 @@ youjia:
# 日志目录(本地兜底;Nacos 上 youjia-dev.yaml 里的 youjia.log.home 优先级更高)
# 日志目录(本地兜底;Nacos 上 youjia-dev.yaml 里的 youjia.log.home 优先级更高)
log
:
log
:
home
:
\data\java_log\jetty
home
:
\data\java_log\jetty
# 跨域(CORS):前缀 youjia.cors,生产建议收紧 allowed-origins 为前端域名白名单
# 这里本地默认全开放(与改造前一致);Nacos 上 youjia-dev.yaml 同名配置优先级更高
cors
:
enabled
:
true
path-patterns
:
-
/**
allowed-origins
:
-
'
*'
allowed-methods
:
[
GET
,
POST
,
OPTIONS
]
allowed-headers
:
[
'
*'
]
exposed-headers
:
[
X-Sign
,
X-Sign-Algorithm
,
X-Sign-Timestamp
,
X-Sign-Nonce
]
allow-credentials
:
true
max-age
:
3600
api-sign
:
api-sign
:
# 开发环境:宽松模式,Swagger / Postman 可直接调试;
# 开发环境:宽松模式,Swagger / Postman 可直接调试;
# 需要验证严格拦截效果时启动追加 --youjia.api-sign.mode=STRICT
# 需要验证严格拦截效果时启动追加 --youjia.api-sign.mode=STRICT
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment