Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
C
cocktail-party-server
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
cocktail-party
cocktail-party-server
Commits
7558c176
提交
7558c176
authored
9月 02, 2025
作者:
RuoYi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
支持防盗链功能
上级
4a5b0e60
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
107 行增加
和
0 行删除
+107
-0
application.yml
ruoyi-admin/src/main/resources/application.yml
+7
-0
RefererFilter.java
.../src/main/java/com/ruoyi/common/filter/RefererFilter.java
+78
-0
FilterConfig.java
...rc/main/java/com/ruoyi/framework/config/FilterConfig.java
+22
-0
没有找到文件。
ruoyi-admin/src/main/resources/application.yml
浏览文件 @
7558c176
...
@@ -119,6 +119,13 @@ swagger:
...
@@ -119,6 +119,13 @@ swagger:
# 请求前缀
# 请求前缀
pathMapping
:
/dev-api
pathMapping
:
/dev-api
# 防盗链配置
referer
:
# 防盗链开关
enabled
:
false
# 允许的域名列表
allowed-domains
:
localhost,127.0.0.1,ruoyi.vip,www.ruoyi.vip
# 防止XSS攻击
# 防止XSS攻击
xss
:
xss
:
# 过滤开关
# 过滤开关
...
...
ruoyi-common/src/main/java/com/ruoyi/common/filter/RefererFilter.java
0 → 100644
浏览文件 @
7558c176
package
com
.
ruoyi
.
common
.
filter
;
import
java.io.IOException
;
import
java.util.Arrays
;
import
java.util.List
;
import
javax.servlet.Filter
;
import
javax.servlet.FilterChain
;
import
javax.servlet.FilterConfig
;
import
javax.servlet.ServletException
;
import
javax.servlet.ServletRequest
;
import
javax.servlet.ServletResponse
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
/**
* 防盗链过滤器
*
* @author ruoyi
*/
public
class
RefererFilter
implements
Filter
{
/**
* 允许的域名列表
*/
public
List
<
String
>
allowedDomains
;
@Override
public
void
init
(
FilterConfig
filterConfig
)
throws
ServletException
{
String
domains
=
filterConfig
.
getInitParameter
(
"allowedDomains"
);
this
.
allowedDomains
=
Arrays
.
asList
(
domains
.
split
(
","
));
}
@Override
public
void
doFilter
(
ServletRequest
request
,
ServletResponse
response
,
FilterChain
chain
)
throws
IOException
,
ServletException
{
HttpServletRequest
req
=
(
HttpServletRequest
)
request
;
HttpServletResponse
resp
=
(
HttpServletResponse
)
response
;
String
referer
=
req
.
getHeader
(
"Referer"
);
// 如果Referer为空,拒绝访问
if
(
referer
==
null
||
referer
.
isEmpty
())
{
resp
.
sendError
(
HttpServletResponse
.
SC_FORBIDDEN
,
"Access denied: Referer header is required"
);
return
;
}
// 检查Referer是否在允许的域名列表中
boolean
allowed
=
false
;
for
(
String
domain
:
allowedDomains
)
{
if
(
referer
.
contains
(
domain
))
{
allowed
=
true
;
break
;
}
}
// 根据检查结果决定是否放行
if
(
allowed
)
{
chain
.
doFilter
(
request
,
response
);
}
else
{
resp
.
sendError
(
HttpServletResponse
.
SC_FORBIDDEN
,
"Access denied: Referer '"
+
referer
+
"' is not allowed"
);
}
}
@Override
public
void
destroy
()
{
}
}
\ No newline at end of file
ruoyi-framework/src/main/java/com/ruoyi/framework/config/FilterConfig.java
浏览文件 @
7558c176
...
@@ -8,6 +8,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
...
@@ -8,6 +8,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import
org.springframework.boot.web.servlet.FilterRegistrationBean
;
import
org.springframework.boot.web.servlet.FilterRegistrationBean
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Configuration
;
import
com.ruoyi.common.constant.Constants
;
import
com.ruoyi.common.filter.RefererFilter
;
import
com.ruoyi.common.filter.RepeatableFilter
;
import
com.ruoyi.common.filter.RepeatableFilter
;
import
com.ruoyi.common.filter.XssFilter
;
import
com.ruoyi.common.filter.XssFilter
;
import
com.ruoyi.common.utils.StringUtils
;
import
com.ruoyi.common.utils.StringUtils
;
...
@@ -26,6 +28,9 @@ public class FilterConfig
...
@@ -26,6 +28,9 @@ public class FilterConfig
@Value
(
"${xss.urlPatterns}"
)
@Value
(
"${xss.urlPatterns}"
)
private
String
urlPatterns
;
private
String
urlPatterns
;
@Value
(
"${referer.allowed-domains}"
)
private
String
allowedDomains
;
@SuppressWarnings
({
"rawtypes"
,
"unchecked"
})
@SuppressWarnings
({
"rawtypes"
,
"unchecked"
})
@Bean
@Bean
@ConditionalOnProperty
(
value
=
"xss.enabled"
,
havingValue
=
"true"
)
@ConditionalOnProperty
(
value
=
"xss.enabled"
,
havingValue
=
"true"
)
...
@@ -43,6 +48,23 @@ public class FilterConfig
...
@@ -43,6 +48,23 @@ public class FilterConfig
return
registration
;
return
registration
;
}
}
@SuppressWarnings
({
"rawtypes"
,
"unchecked"
})
@Bean
@ConditionalOnProperty
(
value
=
"referer.enabled"
,
havingValue
=
"true"
)
public
FilterRegistrationBean
refererFilterRegistration
()
{
FilterRegistrationBean
registration
=
new
FilterRegistrationBean
();
registration
.
setDispatcherTypes
(
DispatcherType
.
REQUEST
);
registration
.
setFilter
(
new
RefererFilter
());
registration
.
addUrlPatterns
(
Constants
.
RESOURCE_PREFIX
+
"/*"
);
registration
.
setName
(
"refererFilter"
);
registration
.
setOrder
(
FilterRegistrationBean
.
HIGHEST_PRECEDENCE
);
Map
<
String
,
String
>
initParameters
=
new
HashMap
<
String
,
String
>();
initParameters
.
put
(
"allowedDomains"
,
allowedDomains
);
registration
.
setInitParameters
(
initParameters
);
return
registration
;
}
@SuppressWarnings
({
"rawtypes"
,
"unchecked"
})
@SuppressWarnings
({
"rawtypes"
,
"unchecked"
})
@Bean
@Bean
public
FilterRegistrationBean
someFilterRegistration
()
public
FilterRegistrationBean
someFilterRegistration
()
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论