簡體   English   中英

使用Jsch檢查SFTP權限

[英]Checking SFTP permissions using Jsch

我目前正在使用Jsch實現SFTP客戶端。

對於此客戶端,我需要檢查登錄用戶在SFTP服務器上具有的權限,以檢查用戶是否能夠執行某些操作。 不幸的是,我似乎無法找到任何文檔或示例,其中顯示了如何做到這一點。

提前致謝。

此代碼將執行您想要的操作:

ChannelSftp channel = (ChannelSftp)session.openChannel("sftp");
SftpATTRS attrs = channel.lstat(fileOnServer)
boolean userHasPermissionsToWriteFile = attrs != null && ((attrs.getPermissions() & 00200) != 0) && attrs.getUId() != 0; 

哪里

attrs.getUId() != 0

檢查用戶不是root用戶

ready方法中的相同方法(格式化和重構):

  private boolean accessAllowed(String path) 
    throws SftpException, JSchException {

    boolean result = false;

    ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
    SftpATTRS attrs = channel.lstat(path);

    result =
      attrs != null &&
      ((attrs.getPermissions() & 00200) != 0) &&
       attrs.getUId() != 0;

    return result;
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM