简体   繁体   English

使用Jsch检查SFTP权限

[英]Checking SFTP permissions using Jsch

I'm currently implementing a SFTP client using Jsch. 我目前正在使用Jsch实现SFTP客户端。

For this client I need to check the permissions the logged in user has on the SFTP server in order to check if user is able to perform certain operations. 对于此客户端,我需要检查登录用户在SFTP服务器上具有的权限,以检查用户是否能够执行某些操作。 Unfortunately I can't seem to find any documentation or examples in which is shown how to to this. 不幸的是,我似乎无法找到任何文档或示例,其中显示了如何做到这一点。

Thanks in advance. 提前致谢。

This code will do what you want: 此代码将执行您想要的操作:

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

Where 哪里

attrs.getUId() != 0

checks that user is not root 检查用户不是root用户

Same approach in a ready method (formatted and refactored): 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