Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
F
fgqyxxlr
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
yaru
fgqyxxlr
Commits
39b063ff
Commit
39b063ff
authored
Sep 30, 2019
by
RuoYi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
去掉jsoup调用自定义转义工具
parent
2d07fcb4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
154 additions
and
17 deletions
+154
-17
pom.xml
+0
-8
ruoyi-common/pom.xml
+0
-6
ruoyi-common/src/main/java/com/ruoyi/common/utils/html/EscapeUtil.java
+152
-0
ruoyi-common/src/main/java/com/ruoyi/common/xss/XssHttpServletRequestWrapper.java
+2
-3
No files found.
pom.xml
View file @
39b063ff
...
...
@@ -29,7 +29,6 @@
<oshi.version>
3.9.1
</oshi.version>
<commons.io.version>
2.5
</commons.io.version>
<commons.fileupload.version>
1.3.3
</commons.fileupload.version>
<jsoup.version>
1.11.3
</jsoup.version>
<poi.version>
3.17
</poi.version>
<velocity.version>
1.7
</velocity.version>
</properties>
...
...
@@ -148,13 +147,6 @@
<version>
${commons.fileupload.version}
</version>
</dependency>
<!-- HTML解析器 -->
<dependency>
<groupId>
org.jsoup
</groupId>
<artifactId>
jsoup
</artifactId>
<version>
${jsoup.version}
</version>
</dependency>
<!-- excel工具 -->
<dependency>
<groupId>
org.apache.poi
</groupId>
...
...
ruoyi-common/pom.xml
View file @
39b063ff
...
...
@@ -77,12 +77,6 @@
<artifactId>
commons-fileupload
</artifactId>
</dependency>
<!-- HTML解析器 -->
<dependency>
<groupId>
org.jsoup
</groupId>
<artifactId>
jsoup
</artifactId>
</dependency>
<!-- excel工具 -->
<dependency>
<groupId>
org.apache.poi
</groupId>
...
...
ruoyi-common/src/main/java/com/ruoyi/common/utils/html/EscapeUtil.java
0 → 100644
View file @
39b063ff
package
com
.
ruoyi
.
common
.
utils
.
html
;
import
com.ruoyi.common.utils.StringUtils
;
/**
* 转义和反转义工具类
*
* @author ruoyi
*/
public
class
EscapeUtil
{
public
static
final
String
RE_HTML_MARK
=
"(<[^<]*?>)|(<[\\s]*?/[^<]*?>)|(<[^<]*?/[\\s]*?>)"
;
private
static
final
char
[][]
TEXT
=
new
char
[
64
][];
static
{
for
(
int
i
=
0
;
i
<
64
;
i
++)
{
TEXT
[
i
]
=
new
char
[]
{
(
char
)
i
};
}
// special HTML characters
TEXT
[
'\''
]
=
"'"
.
toCharArray
();
// 单引号
TEXT
[
'"'
]
=
"""
.
toCharArray
();
// 单引号
TEXT
[
'&'
]
=
"&"
.
toCharArray
();
// &符
TEXT
[
'<'
]
=
"<"
.
toCharArray
();
// 小于号
TEXT
[
'>'
]
=
">"
.
toCharArray
();
// 大于号
}
/**
* 转义文本中的HTML字符为安全的字符
*
* @param text 被转义的文本
* @return 转义后的文本
*/
public
static
String
escape
(
String
text
)
{
return
encode
(
text
);
}
/**
* 还原被转义的HTML特殊字符
*
* @param content 包含转义符的HTML内容
* @return 转换后的字符串
*/
public
static
String
unescape
(
String
content
)
{
return
decode
(
content
);
}
/**
* 清除所有HTML标签,但是不删除标签内的内容
*
* @param content 文本
* @return 清除标签后的文本
*/
public
static
String
clean
(
String
content
)
{
return
content
.
replaceAll
(
RE_HTML_MARK
,
""
);
}
/**
* Escape编码
*
* @param text 被编码的文本
* @return 编码后的字符
*/
private
static
String
encode
(
String
text
)
{
int
len
;
if
((
text
==
null
)
||
((
len
=
text
.
length
())
==
0
))
{
return
StringUtils
.
EMPTY
;
}
StringBuilder
buffer
=
new
StringBuilder
(
len
+
(
len
>>
2
));
char
c
;
for
(
int
i
=
0
;
i
<
len
;
i
++)
{
c
=
text
.
charAt
(
i
);
if
(
c
<
64
)
{
buffer
.
append
(
TEXT
[
c
]);
}
else
{
buffer
.
append
(
c
);
}
}
return
buffer
.
toString
();
}
/**
* Escape解码
*
* @param content 被转义的内容
* @return 解码后的字符串
*/
public
static
String
decode
(
String
content
)
{
if
(
StringUtils
.
isEmpty
(
content
))
{
return
content
;
}
StringBuilder
tmp
=
new
StringBuilder
(
content
.
length
());
int
lastPos
=
0
,
pos
=
0
;
char
ch
;
while
(
lastPos
<
content
.
length
())
{
pos
=
content
.
indexOf
(
"%"
,
lastPos
);
if
(
pos
==
lastPos
)
{
if
(
content
.
charAt
(
pos
+
1
)
==
'u'
)
{
ch
=
(
char
)
Integer
.
parseInt
(
content
.
substring
(
pos
+
2
,
pos
+
6
),
16
);
tmp
.
append
(
ch
);
lastPos
=
pos
+
6
;
}
else
{
ch
=
(
char
)
Integer
.
parseInt
(
content
.
substring
(
pos
+
1
,
pos
+
3
),
16
);
tmp
.
append
(
ch
);
lastPos
=
pos
+
3
;
}
}
else
{
if
(
pos
==
-
1
)
{
tmp
.
append
(
content
.
substring
(
lastPos
));
lastPos
=
content
.
length
();
}
else
{
tmp
.
append
(
content
.
substring
(
lastPos
,
pos
));
lastPos
=
pos
;
}
}
}
return
tmp
.
toString
();
}
public
static
void
main
(
String
[]
args
)
{
String
html
=
"<script>alert(1);</script>"
;
System
.
out
.
println
(
EscapeUtil
.
clean
(
html
));
System
.
out
.
println
(
EscapeUtil
.
escape
(
html
));
System
.
out
.
println
(
EscapeUtil
.
unescape
(
html
));
}
}
ruoyi-common/src/main/java/com/ruoyi/common/xss/XssHttpServletRequestWrapper.java
View file @
39b063ff
...
...
@@ -2,8 +2,7 @@ package com.ruoyi.common.xss;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletRequestWrapper
;
import
org.jsoup.Jsoup
;
import
org.jsoup.safety.Whitelist
;
import
com.ruoyi.common.utils.html.EscapeUtil
;
/**
* XSS过滤处理
...
...
@@ -31,7 +30,7 @@ public class XssHttpServletRequestWrapper extends HttpServletRequestWrapper
for
(
int
i
=
0
;
i
<
length
;
i
++)
{
// 防xss攻击和过滤前后空格
escapseValues
[
i
]
=
Jsoup
.
clean
(
values
[
i
],
Whitelist
.
relaxed
()
).
trim
();
escapseValues
[
i
]
=
EscapeUtil
.
clean
(
values
[
i
]
).
trim
();
}
return
escapseValues
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
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 comment