如何使用带有SFTP协议的JSch检查读取权限? [英] How to check for read permission using JSch with SFTP protocol?

查看:224
本文介绍了如何使用带有SFTP协议的JSch检查读取权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有getPermissions()方法,但是我不知道如何使用它.如果用户可以读取文件,如何使用JSch进行检查?

I know there is getPermissions() method but I don't know how to use it. How can I check using JSch, if user can read files?

推荐答案

首先,通常应该问一个功能性问题(要达到什么目的),以获得有用的答案.您要求实现/技术细节,因此我可能没有用的技术答案:

First, you should generally ask a functional question (what do you want to achive), to get an useful answer. You ask for an implementation/technical detail, hence my possibly useless technical answer:

SftpATTRS.getPermissions()返回* nix权限的数字表示形式:
https://en.wikipedia.org/wiki/File_system_permissions#Numeric_notation

SftpATTRS.getPermissions() returns numerical representation of *nix permissions:
https://en.wikipedia.org/wiki/File_system_permissions#Numeric_notation

仅凭它本身无法确定当前用户是否具有读取文件的权限.另外,您还必须知道:谁是文件的所有者(getUId返回所有者ID,但是SFTP中没有API可以将其映射到用户名/从用户名映射),文件的组是什么(getGId ,相同).

It's on its own NOT enough to determine, if the current user has permissions to read the file. In addition you have to know: who is the owner of the file (the getUId returns owner ID, but there's no API in SFTP to map that to/from username) and what is a group of the file (the getGId, the same).

因此,只有在每个人都有权限时,您才能确保用户可以读取文件.标记0004的存在可以告诉您什么(其他类的读取权限).但是缺少该标志并不意味着用户无法读取该文件,例如如果用户是文件的所有者,并且有标记0400(所有者类的读取权限).

So you can only be sure that user can read the file, when everyone have permissions to do so. What you can tell by presence of flag 0004 (read permissions for other class). But lack of the flag does not mean user cannot read the file, e.g. in case user is an owner of the file and there's flag 0400 (read permissions for owner class).

还要注意,当远程系统不是* nix(例如Windows)时,getPermissions()值通常无关紧要.

Also note that, when the remote system is not *nix (e.g. it's Windows), the getPermissions() value is usually irrelevant anyway.

非平凡但唯一可靠的方法是挖掘JSch源代码并提取新的API来打开文件以供读取.然后,您可以尝试打开文件(实际上不必读取任何内容)以告知用户是否有权这样做.

Non-trivial, but only reliable way, is to dig into JSch source code and extract new API to open file for reading. Then you can just try to open the file (you do not have to read anything actually) to tell, if user has permissions to do so.

这篇关于如何使用带有SFTP协议的JSch检查读取权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆