授予文件新的权限---ACL

这篇博客介绍了如何使用Java NIO2的ACL功能来授予文件新权限的步骤,包括查找用户主体、获取ACL视图、创建新权限条目、插入权限、最后重写ACL,以允许用户进行读取和追加数据操作。
摘要由CSDN通过智能技术生成
  • 步骤:
    授予新权限的步骤:
     * 	1) 调用FileSystem.getUserPrincipalLookupService()方法查看
     * 	2) 获得 ACL 视图
     * 	3) 使用 AclEntry.Builder 对象创建一个新的实体
     * 	4) 读取 ACL
     * 	5) 插入新的实体
     * 	6) 使用 setAcl() 或 setAttribute() 重写 ACL

  • 代码
    Path path = Paths.get("E:/hsm.sql");
    		
    try {
    	//1 lookup for the principal
    	UserPrincipal user = path.getFileSystem().getUserPrincipalLookupService().lookupPrincipalByName("icer");
    			
    	//2 get the ACL view
    	AclFileAttributeView view = Files.getFileAttributeView(path, AclFileAttributeView.class);
    			
    	//3 create a new entry
    	AclEntry entry = AclEntry.newBuilder().setType(AclEntryType.ALLOW)
    					.setPrincipal(user).setPermissions(AclEntryPermission.READ_DATA,
    							AclEntryPermission.APPEND_DATA).build();
    			
    	//4 read acl
    	List<AclEntry> acl = view.getAcl();
    			
    	//5 Insert the new entry
    	acl.add(0, entry);
    			
    	//6.1 rewrite acl
    //	view.setAcl(acl);
    			
    	//6.2rewrite acl
    	Files.setAttribute(path, "acl:acl", acl, LinkOption.NOFOLLOW_LINKS);
    } catch (Exception e) {
    	e.printStackTrace();
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值