Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
sureness
Sureness
提交
d37ebe12
Sureness
项目概览
sureness
/
Sureness
9 个月 前同步成功
通知
32
Star
813
Fork
161
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Sureness
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
d37ebe12
编写于
9月 07, 2021
作者:
sinat_25235033
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
misjudging diff permission configurations as same config (#133)
* fix the diff between pathNode and methodNode
上级
e9e8ac71
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
61 addition
and
29 deletion
+61
-29
core/src/main/java/com/usthe/sureness/matcher/util/TirePathTree.java
...in/java/com/usthe/sureness/matcher/util/TirePathTree.java
+53
-27
core/src/test/java/com/usthe/sureness/matcher/util/TirePathTreeTest.java
...ava/com/usthe/sureness/matcher/util/TirePathTreeTest.java
+8
-2
未找到文件。
core/src/main/java/com/usthe/sureness/matcher/util/TirePathTree.java
浏览文件 @
d37ebe12
...
...
@@ -31,8 +31,9 @@ public class TirePathTree {
private
static
final
String
NODE_TYPE_METHOD
=
"methodNode"
;
private
static
final
String
NODE_TYPE_FILTER_ROLES
=
"rolesNode"
;
private
static
final
String
URL_PATH_SPLIT
=
"/"
;
private
static
final
String
MATCH_ONE
=
"*"
;
private
static
final
String
MATCH_ALL
=
"**"
;
private
static
final
String
MATCH_ALL_METHOD
=
"*"
;
private
static
final
String
MATCH_ONE_PATH
=
"*"
;
private
static
final
String
MATCH_MULTI_PATH
=
"**"
;
private
static
final
int
PATH_NODE_NUM_3
=
3
;
private
static
final
int
PATH_NODE_NUM_2
=
2
;
private
static
final
Pattern
PATH_SPLIT_PATTERN
=
Pattern
.
compile
(
"/+"
);
...
...
@@ -108,8 +109,8 @@ public class TirePathTree {
resourceList
.
add
(
root
);
while
(!
resourceList
.
isEmpty
())
{
Node
currentNode
=
resourceList
.
poll
();
if
(
NODE_TYPE_METHOD
.
equals
(
currentNode
.
nodeType
))
{
resourceNum
+
+
;
if
(
currentNode
.
getMethodChildren
()
!=
null
&&
!
currentNode
.
getMethodChildren
().
isEmpty
(
))
{
resourceNum
+
=
currentNode
.
getMethodChildren
().
size
()
;
}
if
(
currentNode
.
getChildren
()
!=
null
&&
!
currentNode
.
getChildren
().
isEmpty
())
{
resourceList
.
addAll
(
currentNode
.
getChildren
().
values
());
...
...
@@ -171,22 +172,28 @@ public class TirePathTree {
return
null
;
}
if
(
currentFlow
==
urlPac
.
length
-
1
&&
(
NODE_TYPE_MAY_PATH_END
.
equals
(
current
.
getNodeType
())))
{
Node
methodNode
=
current
.
get
Children
().
get
(
method
);
Node
methodNode
=
current
.
get
MethodChild
(
method
);
if
(
methodNode
!=
null
)
{
if
(
NODE_TYPE_METHOD
.
equals
(
methodNode
.
getNodeType
()))
{
return
methodNode
.
getChildren
().
keySet
().
iterator
().
next
();
}
}
else
{
Node
nextNode
=
current
.
get
Children
().
get
(
MATCH_ONE
);
Node
nextNode
=
current
.
get
MethodChild
(
MATCH_ALL_METHOD
);
if
(
nextNode
!=
null
&&
NODE_TYPE_METHOD
.
equals
(
nextNode
.
getNodeType
()))
{
return
nextNode
.
getChildren
().
keySet
().
iterator
().
next
();
}
if
(
nextNode
==
null
)
{
nextNode
=
current
.
getChildren
().
get
(
MATCH_ALL
);
nextNode
=
current
.
getChildren
().
get
(
MATCH_ONE_PATH
);
if
(
nextNode
!=
null
&&
NODE_TYPE_MAY_PATH_END
.
equals
(
nextNode
.
getNodeType
()))
{
methodNode
=
nextNode
.
getMethodChild
(
method
);
methodNode
=
methodNode
==
null
?
nextNode
.
getMethodChild
(
MATCH_ALL_METHOD
)
:
methodNode
;
if
(
methodNode
!=
null
&&
NODE_TYPE_METHOD
.
equals
(
methodNode
.
getNodeType
()))
{
return
methodNode
.
getChildren
().
keySet
().
iterator
().
next
();
}
}
nextNode
=
current
.
getChildren
().
get
(
MATCH_MULTI_PATH
);
if
(
nextNode
!=
null
&&
NODE_TYPE_MAY_PATH_END
.
equals
(
nextNode
.
getNodeType
()))
{
methodNode
=
nextNode
.
get
Children
().
get
(
method
);
methodNode
=
methodNode
==
null
?
nextNode
.
get
Children
().
get
(
MATCH_ONE
)
:
methodNode
;
methodNode
=
nextNode
.
get
MethodChild
(
method
);
methodNode
=
methodNode
==
null
?
nextNode
.
get
MethodChild
(
MATCH_ALL_METHOD
)
:
methodNode
;
if
(
methodNode
!=
null
&&
NODE_TYPE_METHOD
.
equals
(
methodNode
.
getNodeType
()))
{
return
methodNode
.
getChildren
().
keySet
().
iterator
().
next
();
}
...
...
@@ -201,7 +208,7 @@ public class TirePathTree {
return
matchRole
;
}
}
if
(
currentNodeData
.
equals
(
MATCH_ONE
))
{
if
(
currentNodeData
.
equals
(
MATCH_ONE
_PATH
))
{
matchRole
=
searchPathRoleInChildren
(
current
,
urlPac
,
currentFlow
-
1
,
method
);
if
(
matchRole
!=
null
)
{
return
matchRole
;
...
...
@@ -211,7 +218,7 @@ public class TirePathTree {
return
matchRole
;
}
}
if
(
currentNodeData
.
equals
(
MATCH_
ALL
))
{
if
(
currentNodeData
.
equals
(
MATCH_
MULTI_PATH
))
{
matchRole
=
searchPathRoleInChildren
(
current
,
urlPac
,
currentFlow
-
1
,
method
);
if
(
matchRole
!=
null
)
{
return
matchRole
;
...
...
@@ -257,15 +264,15 @@ public class TirePathTree {
}
}
}
if
(
current
.
getChildren
().
containsKey
(
MATCH_ONE
))
{
Node
matchOneNode
=
current
.
getChildren
().
get
(
MATCH_ONE
);
if
(
current
.
getChildren
().
containsKey
(
MATCH_ONE
_PATH
))
{
Node
matchOneNode
=
current
.
getChildren
().
get
(
MATCH_ONE
_PATH
);
matchRole
=
searchPathRole
(
matchOneNode
,
urlPac
,
currentFlow
+
1
,
method
);
if
(
matchRole
!=
null
)
{
return
matchRole
;
}
}
if
(
current
.
getChildren
().
containsKey
(
MATCH_
ALL
))
{
Node
matchAllNode
=
current
.
getChildren
().
get
(
MATCH_
ALL
);
if
(
current
.
getChildren
().
containsKey
(
MATCH_
MULTI_PATH
))
{
Node
matchAllNode
=
current
.
getChildren
().
get
(
MATCH_
MULTI_PATH
);
matchRole
=
searchPathRole
(
matchAllNode
,
urlPac
,
currentFlow
+
1
,
method
);
}
return
matchRole
;
...
...
@@ -284,8 +291,8 @@ public class TirePathTree {
if
(
pattern
==
null
||
pathNode
==
null
)
{
return
true
;
}
if
(
pattern
.
equals
(
pathNode
)
||
MATCH_ONE
.
equals
(
pattern
)
||
MATCH_
ALL
.
equals
(
pattern
))
{
if
(
pattern
.
equals
(
pathNode
)
||
MATCH_ONE
_PATH
.
equals
(
pattern
)
||
MATCH_
MULTI_PATH
.
equals
(
pattern
))
{
return
false
;
}
if
(!
isPatternStr
(
pattern
))
{
...
...
@@ -307,9 +314,9 @@ public class TirePathTree {
*/
private
boolean
isPatternStr
(
String
str
)
{
return
str
!=
null
&&
str
.
contains
(
MATCH_ONE
)
&&
!
MATCH_ONE
.
equals
(
str
)
&&
!
MATCH_
ALL
.
equals
(
str
);
&&
str
.
contains
(
MATCH_ONE
_PATH
)
&&
!
MATCH_ONE
_PATH
.
equals
(
str
)
&&
!
MATCH_
MULTI_PATH
.
equals
(
str
);
}
/**
...
...
@@ -372,7 +379,7 @@ public class TirePathTree {
pre
=
current
;
current
=
current
.
getChildren
().
get
(
urlData
);
}
if
(
MATCH_ONE
.
equals
(
current
.
getData
())
||
MATCH_ALL
.
equals
(
current
.
getData
()))
{
if
(
MATCH_ONE
_PATH
.
equals
(
current
.
getData
())
||
MATCH_MULTI_PATH
.
equals
(
current
.
getData
()))
{
// When the last one is * or **, it may match empty,
// and the previous one may also be NODE_TYPE_MAY_PATH_END type
pre
.
setNodeType
(
NODE_TYPE_MAY_PATH_END
);
...
...
@@ -380,13 +387,13 @@ public class TirePathTree {
// set node type is NODE_TYPE_MAY_PATH_END
current
.
setNodeType
(
NODE_TYPE_MAY_PATH_END
);
// start insert httpMethod method, if existed, not overwrite and modify the original configuration
if
(!
current
.
getChildren
().
containsKey
(
method
)
&&
!
current
.
getChildren
().
containsKey
(
MATCH_ONE
))
{
current
.
insertChild
(
method
,
NODE_TYPE_METHOD
);
}
else
{
logger
.
warn
(
"[sureness]-The path resource: {} has match same method or *, ignore it."
,
path
);
if
(
current
.
getMethodChildren
()
!=
null
&&
(
current
.
getMethodChildren
().
containsKey
(
method
)
||
current
.
getMethodChildren
().
containsKey
(
MATCH_ALL_METHOD
)))
{
logger
.
warn
(
"[sureness]-The path resource: {} has match same method, ignore it."
,
path
);
return
;
}
current
=
current
.
getChildren
().
get
(
method
);
current
=
current
.
insertMethodChildren
(
method
);
// Start inserting leaf nodes - supportRoles
// each resource only mapping a left node, that is, at most one child node under supportRoles--httpMethod
// if existed, not overwrite and modify the original configuration
...
...
@@ -414,6 +421,8 @@ public class TirePathTree {
/** pattern children list **/
private
List
<
String
>
patternChildren
;
private
Map
<
String
,
Node
>
methodChildren
;
private
Node
(
String
data
,
String
nodeType
)
{
this
.
data
=
data
;
this
.
nodeType
=
nodeType
;
...
...
@@ -440,6 +449,23 @@ public class TirePathTree {
this
.
patternChildren
.
add
(
data
);
}
public
Node
getMethodChild
(
String
method
)
{
return
methodChildren
==
null
?
null
:
methodChildren
.
get
(
method
);
}
public
Map
<
String
,
Node
>
getMethodChildren
()
{
return
methodChildren
;
}
public
Node
insertMethodChildren
(
String
method
)
{
if
(
methodChildren
==
null
)
{
methodChildren
=
new
HashMap
<>(
8
);
}
Node
node
=
new
Node
(
method
,
NODE_TYPE_METHOD
);
methodChildren
.
put
(
method
,
node
);
return
node
;
}
private
String
getNodeType
()
{
return
nodeType
;
}
...
...
core/src/test/java/com/usthe/sureness/matcher/util/TirePathTreeTest.java
浏览文件 @
d37ebe12
...
...
@@ -78,9 +78,12 @@ public class TirePathTreeTest {
// support ignore http method
paths
.
add
(
"/api/school/book===*===[role8]"
);
paths
.
add
(
"/api2/school/*===*===[role18]"
);
// fix issue bug https://github.com/dromara/sureness/issues/132
paths
.
add
(
"/api/v6/book/*===get===[role1]"
);
paths
.
add
(
"/api/v6/book===post===[role2]"
);
root
.
buildTree
(
paths
);
assertEquals
(
3
3
,
root
.
getResourceNum
());
assertEquals
(
3
5
,
root
.
getResourceNum
());
}
@Test
...
...
@@ -137,6 +140,9 @@ public class TirePathTreeTest {
assertEquals
(
"[role18]"
,
root
.
searchPathFilterRoles
(
"/api2/school/book===post"
));
assertEquals
(
"[role18]"
,
root
.
searchPathFilterRoles
(
"/api2/school/student===get"
));
assertEquals
(
"[role18]"
,
root
.
searchPathFilterRoles
(
"/api2/school===delete"
));
// fix issue bug https://github.com/dromara/sureness/issues/132
assertEquals
(
"[role1]"
,
root
.
searchPathFilterRoles
(
"/api/v6/book/3===get"
));
assertEquals
(
"[role2]"
,
root
.
searchPathFilterRoles
(
"/api/v6/book===post"
));
}
}
\ No newline at end of file
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录