CSP角色授权(Java)

本文介绍了一种使用Java实现的模拟权限验证系统,通过角色、用户及用户组管理权限,利用Set和Map进行高效的数据管理和查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这道题应该还是很常规的模拟大题,不过我却花了整整两天找bug(一直运行错误,哭晕.jpg),至于为什么等会再说,我先说说这道题的思路。
角色对象:三个Set,分别存操作、资源类型、资源名称,并去重。
连接对象:一个Set,存角色名称
对于用户和用户组分别都加上一个连接对象,Map用来快速定位。


import java.io.*;
import java.util.*;


public class Main {

    public static void main(String[] args)  {
        Scanner in =new Scanner(System.in);
        int n=in.nextInt();
        int m=in.nextInt();
        int q=in.nextInt();
        //map key:角色名字 value:角色对象 ,用来根据名字快速定位
        Map<String,role> map=new HashMap<>();
        //添加角色模块
        while (n-->0){
            String name=in.next();
            role r=new role(name);
            int nv=in.nextInt();
            //将操作、资源类型、资源名称,分别加入对应的Set集合
            while (nv-->0){
                String op=in.next();
                r.setop.add(op);
            }
            int no=in.nextInt();
            while (no-->0){
                String doc=in.next();
                r.setdoc.add(doc);
            }
            int nn=in.nextInt();
            while (nn-->0){
                String file=in.next();
                r.setfile.add(file);
            }

            map.put(name,r);
        }
        Map<String,cont> people=new HashMap<>();//存用户
        Map<String,cont> group =new HashMap<>();//存用户组
        //添加用户和用户组模块
        while (m-->0){
            String name = in.next();
            int ns=in.nextInt();
            while (ns-->0){
                String f=in.next();
                String s=in.next();
                //判别类型分别加入,如果记录过就直接添加到集合中
                if (f.compareTo("g")==0){
                    if (group.containsKey(s)){
                        group.get(s).role.add(name);
                    }else {
                        group.put(s,new cont(name));
                    }
                }else {
                    if (people.containsKey(s)){
                        people.get(s).role.add(name);
                    }else {
                        people.put(s,new cont(name));
                    }
                }

            }
        }
        //实时判断模块
        while (q-->0){
            String name=in.next();

            int ng=in.nextInt();
            String[] ss=new String[ng];
            for (int i=0;i<ng;i++){
                ss[i]=in.next();
            }
            String op=in.next();
            String doc=in.next();
            String file=in.next();

            //判断用户名是否可以通过
            boolean f=false;
            if (people.containsKey(name)){
                for(String rolename:people.get(name).role){
                    if (map.containsKey(rolename)){
                        role r =map.get(rolename);
                        if (r.setop.contains(op)||r.setop.contains("*")){
                            if (r.setdoc.contains(doc)||r.setdoc.contains("*")){
                                if (r.setfile.isEmpty()||r.setfile.contains(file)){
                                    f=true;
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            if (f){
                System.out.println(1);
                continue;
            }
            //判断用户组是否可以通过
            for (String s:ss){
                boolean ff=false;
                if (group.containsKey(s)){
                    for(String rolename:group.get(s).role){//遍历用户组的角色
                        if (map.containsKey(rolename)){
                            role r =map.get(rolename);
                            if (r.setop.contains(op)||r.setop.contains("*")){
                                if (r.setdoc.contains(doc)||r.setdoc.contains("*")){
                                    if (r.setfile.isEmpty()||r.setfile.contains(file)){
                                        f=true;//判断成不成功的
                                        ff=true;//用来判断及时推出的
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
                if (ff)break;
            }
            System.out.println(f?1:0);
        }


    }
}

class role{
    String name;
    Set<String> setop=new HashSet<>();
    Set<String> setdoc=new HashSet<>();
    Set<String> setfile=new HashSet<>();
    role(String name){
        this.name=name;
    }

}
class cont{
    Set<String> role=new HashSet<>();
    cont(String rolename){role.add(rolename);}
}


接下来就是我要强调的一个重点类名不能用windows关键字!!!,当初我把cont类命名为con,本地运行没问题,但是提交后一直是运行错误,然后我就反复查找逻辑错误,最终才发现了这个错误,满满的都是泪啊。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值