首页 > 其他分享 >包含UUID的路径匹配校验

包含UUID的路径匹配校验

时间:2023-01-06 15:33:59浏览次数:53  
标签:uuid 匹配 String idIndex 校验 UUID user server requestPath

请求路径有三种格式:

(1)account_server/v1/users/6f0f1d91-9b76-4499-af64-d05a780c0287/profile【UPDATE】

(2)account_server/v14/users/6f0f1d91-9b76-4499-af64-d05a780c0287【DELETE】

(3)account_server/v1/users【GET】

对应权限表路径格式:

(1)method:UPDATE ;servername:account_server; uri:/users/{id}/profile

(2)method:DELETE;servername:account_server; uri:/users/{id}

(3)method:GET ;servername:account_server; uri:/users

需求梳理:

(1)请求路径需忽略版本编号(剔除版本号功能):基于版本号位置固定,在前两个“/”之间

(2)请求路径的uuid位置需要匹配,格式需要正确

(3)除版本号,UUID外的路径需一致

核心代码

package com.example.demo;

import java.util.List;

public class PathMatches {
    public static boolean validate(String requestPath, String requestMethod, List<Permission> permissions) {
        if (permissions != null) {
            for (Permission permission : permissions) {
                String method = permission.getMethod();
                String uri = permission.getUri();
                String serverName = permission.getServerName();
                String path = serverName + "/" + uri;
                if (validate(path, requestPath) && method.equals(requestMethod)) {
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean validate(String path, String requestPath) {
        requestPath = subVersion(requestPath);
        int idIndex = path.indexOf("{id}");
        int pathLength = path.length();
        int requestPathLength = requestPath.length();
        if (!path.contains("{id}")) {
            //权限路径不包含id
            return path.equals(requestPath);
        } else {
            if (pathLength - idIndex == 4 && requestPathLength >= idIndex + 36) {
                //权限路径id结尾 且 请求路径包含uuid
                String uuid = requestPath.substring(idIndex, idIndex + 36);
                requestPath = subPath(requestPath, idIndex);
                return path.equals(requestPath) && uuid.matches("(\\w{8}(-\\w{4}){3}-\\w{12}?)");
            } else if (requestPathLength >= idIndex + 36) {
                //权限路径id居中 且 请求路径包含uuid
                try {
                    String uuid = requestPath.substring(idIndex, idIndex + 36);
                    requestPath = subPath(requestPath, idIndex);
                    return path.equals(requestPath) && uuid.matches("(\\w{8}(-\\w{4}){3}-\\w{12}?)");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

    public static String subPath(String path, int idIndex) {
        String uuid = path.substring(idIndex, idIndex + 36);
        return path.replace(uuid, "{id}");
    }

    public static String subVersion(String requestMethod) {
        int index = requestMethod.indexOf("/");
        int index2 = requestMethod.indexOf("/", index + 1);
        String version = requestMethod.substring(index, index2);
        return requestMethod.replace(version, "");
    }
}

测试类代码

package com.example.demo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


public class Test {
    static void test1() {
        User user = getUser();
        String uuid = UUID.randomUUID().toString();
        String requestPath = "demo-server/v1/user/" + uuid;
        String method = "GET";
        boolean flag = PathMatches.validate(requestPath, method, user.getPermissions());
        System.out.println(flag);
    }

    static void test2() {
        User user = getUser();
        String uuid = UUID.randomUUID().toString();
        String requestPath = "demo-server/v1/user/" + uuid + "/pour";
        String method = "POST";
        boolean flag = PathMatches.validate(requestPath, method, user.getPermissions());
        System.out.println(flag);
    }

    static void test3() {
        User user = getUser();
        String requestPath = "demo-server/v1/user";
        String method = "DELETE";
        boolean flag = PathMatches.validate(requestPath, method, user.getPermissions());
        System.out.println(flag);
    }

    public static void main(String[] args) {
        test1();
        test2();
        test3();
    }

    private static User getUser() {
        User user = new User();
        user.setRoleId(UUID.randomUUID().toString());
        user.setPermissions(getPermissions());
        return user;
    }

    private static List<Permission> getPermissions() {
        List<Permission> permissions = new ArrayList<>();
        permissions.add(new Permission("GET", "demo-server", "user/{id}"));
        permissions.add(new Permission("POST", "demo-server", "user/{id}/pour"));
        permissions.add(new Permission("DELETE", "demo-server", "user"));
        return permissions;
    }

}


@Data
class User {
    private String roleId;
    private List<Permission> permissions;
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class Permission {
    private String method;
    private String serverName;
    private String uri;
}

 

标签:uuid,匹配,String,idIndex,校验,UUID,user,server,requestPath
From: https://www.cnblogs.com/excellencesy/p/17030604.html

相关文章