提交 1dbca7f3 编写于 作者: sinat_25235033's avatar sinat_25235033

修改tree匹配bug

上级 eca58dbf
......@@ -16,6 +16,12 @@
<jjwt.version>0.9.0</jjwt.version>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
......@@ -32,6 +38,12 @@
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
......
package com.usthe.sureness;
import com.sun.tools.javac.util.StringUtils;
import com.usthe.sureness.matcher.util.TirePathTreeUtil;
import javax.servlet.http.HttpServletRequest;
import java.net.http.HttpRequest;
import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
/**
......@@ -15,10 +18,15 @@ public class HaTest {
public static void main(String[] args) {
String[] ps = "/".split("/");
for (String temp : ps) {
System.out.println(temp);
}
TirePathTreeUtil.Node root = new TirePathTreeUtil.Node("root");
Set<String> paths = new HashSet<>();
paths.add("/===post===jwt[role2,role3,role4]");
paths.add("/api/v2/host===post===jwt[role2,role3,role4]");
paths.add("/api/v2/host===get===jwt[role2,role3,role4]");
paths.add("/api/v2/host===delete===jwt[role2,role3,role4]");
......@@ -45,4 +53,24 @@ public class HaTest {
// System.out.println(ss[0] + ss[1] + ss[2]);
}
public static void ddmain(String[] args){
double x,y,r;
Scanner sc = new Scanner(System.in);
long seed = sc.nextLong();
Random s = new Random(seed);
int n = sc.nextInt();
int m = 0;
for(int i=0;i<n;i++){
x= s.nextDouble() * 2 - 1;
// x= x/(double) n;
y= s.nextDouble() * 2 - 1;
// y = y/(double) n;
if (Math.sqrt(Math.pow(x,2) + Math.pow(y,2)) < 1) {
m++;
}
}
r= 4 * (double)m/(double)n;
System.out.println(r);
}
}
......@@ -16,7 +16,7 @@ import java.util.Set;
*/
public class DefaultPathRoleMatcher implements TreePathRoleMatcher {
private static final Logger LOG = LoggerFactory.getLogger(DefaultPathRoleMatcher.class);
private static final Logger logger = LoggerFactory.getLogger(DefaultPathRoleMatcher.class);
/**
......
......@@ -9,11 +9,6 @@ import com.usthe.sureness.subject.SubjectAuToken;
*/
public interface TreePathRoleMatcher {
/**
* description 通过targetUri 在树种匹配出所支持的roles
*
*/
/**
* description : 通过auToken中的 targetUri 在树种匹配出所支持的roles 填充到token中
* TODO 抛出定制的异常
......
package com.usthe.sureness.matcher.util;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
......@@ -16,27 +17,32 @@ public class TirePathTreeUtil {
private static final String NODE_TYPE_PATH_END = "isPathEnd";
private static final String NODE_TYPE_METHOD = "methodNode";
private static final String NODE_TYPE_FILTER_ROLES = "filterRolesNode";
private static final String URL_PATH_SPLIT = "/";
private static final int PATH_NODE_NUM_3 = 3;
private static final int PATH_NODE_NUM_2 = 2;
/**
* description 插入节点
*
* @param path path = /api/v1/host/detail===GET===jwt[role2,role3,role4]
*/
private static void insertNode(String path, Node root) {
if (path == null || "".equals(path)) {
if (path == null || "".equals(path) || !path.startsWith(URL_PATH_SPLIT)) {
return;
}
String[] tmp = path.split("===");
if (tmp.length != PATH_NODE_NUM_3) {
return;
}
String[] urlPac = tmp[0].split("/");
String[] urlPac = tmp[0].split(URL_PATH_SPLIT);
if (urlPac.length > 1) {
// 去除第一位的空 ""
urlPac = Arrays.copyOfRange(urlPac, 1, urlPac.length);
}
String method = tmp[1];
String filterRoles = tmp[2];
Node current = root;
......@@ -67,7 +73,7 @@ public class TirePathTreeUtil {
*/
public static void buildTree(Set<String> paths, Node root) {
for (String path : paths) {
insertNode(path , root);
insertNode(path, root);
}
}
......@@ -95,7 +101,7 @@ public class TirePathTreeUtil {
* @return java.lang.String
*/
public static String searchPathFilterRoles(String path, Node root) {
if (path == null || "".equals(path)) {
if (path == null || "".equals(path) || !path.startsWith(URL_PATH_SPLIT)) {
return null;
}
String[] tmp = path.split("===");
......@@ -103,6 +109,9 @@ public class TirePathTreeUtil {
return null;
}
String[] urlPac = tmp[0].split("/");
if (urlPac.length > 1) {
urlPac = Arrays.copyOfRange(urlPac, 1, urlPac.length);
}
String method = tmp[1];
Node current = root;
//支持基于ant的模式匹配
......@@ -171,7 +180,7 @@ public class TirePathTreeUtil {
public static class Node {
public Node(String data, String nodeType) {
private Node(String data, String nodeType) {
this.data = data;
this.nodeType = nodeType;
}
......@@ -190,7 +199,7 @@ public class TirePathTreeUtil {
/**
* 孩子节点
*/
private Map<String, Node> children = new HashMap<String, Node>();
private Map<String, Node> children = new HashMap<>();
public void insertChild(String data) {
this.children.put(data,new Node(data));
......
......@@ -77,7 +77,7 @@ public class JwtProcessor extends BaseProcessor {
List<String> ownRoles = (List<String>)var.getOwnRoles();
List<String> supportRoles = (List<String>)var.getSupportRoles();
return supportRoles == null || supportRoles.isEmpty()
|| supportRoles.stream().anyMatch(role -> ownRoles.contains(role));
|| supportRoles.stream().anyMatch(ownRoles::contains);
}
@Override
......
......@@ -16,7 +16,7 @@ import java.util.List;
*/
public class DefaultSubjectFactory implements SubjectFactory {
private static final Logger LOG = LoggerFactory.getLogger(SurenessSecurityManager.class);
private static final Logger logger = LoggerFactory.getLogger(SurenessSecurityManager.class);
private boolean initFlag = false;
......
package com.usthe.sureness.matcher.util;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.HashSet;
import java.util.Set;
/**
* @author tomsun28
* @date 12:22 2019-04-09
*/
public class TirePathTreeUtilTest {
private static TirePathTreeUtil.Node root = null;
@BeforeClass
public static void setUp() throws Exception {
root = new TirePathTreeUtil.Node("root");
}
@AfterClass
public static void tearDown() throws Exception {
root = null;
}
@Test
public void buildTree() {
Set<String> paths = new HashSet<>();
paths.add("/api/v2/host===post===jwt[role2,role3,role4]");
paths.add("/api/v2/host===get===jwt[role2,role3,role4]");
paths.add("/api/v2/host===delete===jwt[role2,role3,role4]");
paths.add("/api/v2/host===put===jwt[role2,role3,role4]");
paths.add("/api/v1/host===put===jwt[role2,role3,role4]");
paths.add("/api/v3/host===put===jwt[role2,role3,role4]");
paths.add("/api/v2/detail===put===jwt[role2,role3,role4]");
paths.add("/api/v2/mom===put===jwt[role2,role3,role4]");
paths.add("/api/*/mom/ha===put===jwt[role2,role3,role4]");
paths.add("/api/mi/**===put===jwt[role2,role3,role4]");
TirePathTreeUtil.buildTree(paths, root);
Assert.assertEquals(root.getChildren().size(), 1);
}
@Test
public void reBuildTree() {
}
@Test
public void clearTree() {
}
@Test
public void searchPathFilterRoles() {
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册