未验证 提交 426ec77b 编写于 作者: sinat_25235033's avatar sinat_25235033 提交者: GitHub

feature ignore path http request match method (#81)

* support ignore http method request match

* docs update: update path match

* fix test fail
上级 e92af60a
......@@ -173,11 +173,15 @@ public class TirePathTree {
}
} else {
Node nextNode = current.getChildren().get(MATCH_ONE);
if (nextNode != null && NODE_TYPE_METHOD.equals(nextNode.getNodeType())) {
return nextNode.getChildren().keySet().iterator().next();
}
if (nextNode == null) {
nextNode = current.getChildren().get(MATCH_ALL);
}
if (nextNode != null && NODE_TYPE_MAY_PATH_END.equals(nextNode.getNodeType())) {
methodNode = nextNode.getChildren().get(method);
methodNode = methodNode == null ? nextNode.getChildren().get(MATCH_ONE) : methodNode;
if (methodNode != null && NODE_TYPE_METHOD.equals(methodNode.getNodeType())) {
return methodNode.getChildren().keySet().iterator().next();
}
......@@ -371,8 +375,11 @@ 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)) {
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);
return;
}
current = current.getChildren().get(method);
// Start inserting leaf nodes - supportRoles
......@@ -380,6 +387,8 @@ public class TirePathTree {
// if existed, not overwrite and modify the original configuration
if (current.getChildren().isEmpty()) {
current.insertChild(supportRoles, NODE_TYPE_FILTER_ROLES);
} else {
logger.warn("[sureness]-The path resource: {} already has supportRoles, ignore it.", path);
}
}
......
......@@ -17,6 +17,7 @@ resourceRole:
- /api/v1/source1===delete===[role3]
- /api/v1/source1===put===[role1,role2]
- /api/v1/source2===get===[]
- /api/v3/source===*===[role2]
# load api resource which do not need be protected, means them need be excluded.
# these api resource can be access by everyone
......@@ -28,6 +29,7 @@ excludedResource:
- /**/*.js===get
- /**/*.css===get
- /**/*.ico===get
- /**/*.png===*
# account info
# there are three account: admin, root, tom
......
......@@ -5,6 +5,7 @@ import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.*;
......@@ -30,7 +31,7 @@ public class TirePathTreeTest {
@Test
public void buildTree() {
Set<String> paths = new HashSet<>();
Set<String> paths = new LinkedHashSet<>();
// '/' path
paths.add("/api///v2////book///node//===get===[]");
// The configuration will not be overwritten or superimposed
......@@ -74,9 +75,12 @@ public class TirePathTreeTest {
paths.add("/**/*.js===post===[role8]");
// lower upper roles
paths.add("/api/role/book===get===[ROLE10]");
// support ignore http method
paths.add("/api/school/book===*===[role8]");
paths.add("/api2/school/*===*===[role18]");
root.buildTree(paths);
assertEquals(31, root.getResourceNum());
assertEquals(33, root.getResourceNum());
}
@Test
......@@ -124,5 +128,15 @@ public class TirePathTreeTest {
assertEquals("[role8]", root.searchPathFilterRoles("/node/v2/demo.css===post"));
// lower upper roles
assertEquals("[ROLE10]", root.searchPathFilterRoles("/api/role/book===get"));
// support ignore http method
assertEquals("[role8]", root.searchPathFilterRoles("/api/school/book===get"));
assertEquals("[role8]", root.searchPathFilterRoles("/api/school/book===post"));
assertEquals("[role8]", root.searchPathFilterRoles("/api/school/book===delete"));
assertEquals("[role8]", root.searchPathFilterRoles("/api/school/book===put"));
assertEquals("[role18]", root.searchPathFilterRoles("/api2/school/book===get"));
assertEquals("[role18]", root.searchPathFilterRoles("/api2/school/book===post"));
assertEquals("[role18]", root.searchPathFilterRoles("/api2/school/student===get"));
assertEquals("[role18]", root.searchPathFilterRoles("/api2/school===delete"));
}
}
\ No newline at end of file
......@@ -24,7 +24,7 @@ class DocumentPathTreeProviderTest {
PathTreeProvider pathTreeProvider = new DocumentPathTreeProvider();
Set<String> paths = pathTreeProvider.providePathData();
assertNotNull(paths);
assertEquals(12, paths.size());
assertEquals(13, paths.size());
}
@Test
......@@ -32,6 +32,6 @@ class DocumentPathTreeProviderTest {
PathTreeProvider pathTreeProvider = new DocumentPathTreeProvider();
Set<String> paths = pathTreeProvider.provideExcludedResource();
assertNotNull(paths);
assertEquals(7, paths.size());
assertEquals(8, paths.size());
}
}
\ No newline at end of file
......@@ -21,6 +21,7 @@ resourceRole:
- /api/v1/source2===get===[]
- /api/v1/source2/*/*===get===[role2]
- /api/v2/source3/*===get===[role2]
- /api/v3/source===*===[role2]
# 需要被过滤保护的资源,不认证鉴权直接访问
# /api/v1/source3===get 表示 /api/v1/source3===get 可以被任何人访问 无需登录认证鉴权
......@@ -31,7 +32,8 @@ excludedResource:
- /**/*.js===get
- /**/*.css===get
- /**/*.ico===get
- /**/*.png===*
# 用户账户信息
# 下面有 admin root tom三个账户
# eg: admin 拥有[role1,role2]角色,明文密码为admin,加盐密码为0192023A7BBD73250516F069DF18B500
......
## URI路径匹配
我们配置的资源格式为:`requestUri===httpMethod`, 即请求的路径加上其请求方式(`post,get,put,delete...`)作为一个整体被视作一个资源
我们配置的资源格式为:`requestUri===httpMethod`, 即请求的路径加上其请求方式(`post,get,put,delete...或者*,*匹配所有请求方式`)作为一个整体被视作一个资源
`eg: /api/v2/book===get` `get`方式请求`/api/v2/book`接口数据
这里的`requestUri`支持url路径匹配符匹配: `str*str`, `*`, `**`
......
......@@ -25,6 +25,7 @@ resourceRole:
- /api/v1/source1===delete===[role3]
- /api/v1/source1===put===[role1,role2]
- /api/v1/source2===get===[]
- /api/v3/source===*===[role2]
# load api resource which do not need be protected, means them need be excluded.
# these api resource can be access by everyone
......@@ -36,6 +37,7 @@ excludedResource:
- /**/*.js===get
- /**/*.css===get
- /**/*.ico===get
- /**/*.png===*
# account info
# there are three account: admin, root, tom
......
## URI Path Match
We treat restful requests as a resource, resource format like `requestUri===httpMethod`.
That is the request uri + request method(`post,get,put,delete...`) is considered as a resource as a whole.
`eg: /api/v2/book===get`
That is the request uri + request method(`post,get,put,delete...or *, * match all request method`) is considered as a resource as a whole.
`eg: /api/v2/book===get, /api/v3/book===*`
The `requestUri` here support url path match: `str*str`, `*`, `**`
| Wildcard | Describe |
......
......@@ -20,6 +20,7 @@ resourceRole:
- /api/v1/source2/*/*===get===[role2]
- /api/v2/source3/*===get===[role2]
- /webSocket/demo===get===[role1]
- /api/v3/source===*===[role2]
# load api resource which do not need be protected, means them need be excluded.
# these api resource can be access by everyone
......@@ -37,6 +38,7 @@ excludedResource:
- /**/*.gif===get
- /swagger-resources/**===get
- /v2/api-docs===get
- /**/*.png===*
# account info
# there are three account: admin, root, tom
......
......@@ -19,3 +19,4 @@ excludedResource:
- /**/*.js===get
- /**/*.css===get
- /**/*.ico===get
- /**/*.png===*
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册