从 Applet 使用 JSch 连接到 SFTP 服务器时出现 AccessControlException

发布于 2024-08-07 17:07:55 字数 4607 浏览 5 评论 0原文

该小程序将从 SFTP 服务器下载文件。 JSch 库用于创建会话、使用它连接到 SFTP 服务器、创建 SFTP 通道并对该服务器上的该文件执行 GET 命令。小程序已签名。

下载文件的代码片段:

    public static void prepareSession() throws JSchException {

  try {
   session = jsch.getSession(user,host,port);
   session.setConfig("StrictHostKeyChecking", "no");        
      session.setPassword(password);
  } catch (JSchException e) {
   e.printStackTrace();
   throw new JSchException(e.getLocalizedMessage(),e);
  }
 }

 public synchronized static void downloadFile() throws Exception {
  ChannelSftp channelSFTP = null;
  try {
   if (!session.isConnected()) {
    session.connect();
   }
   Channel channel=session.openChannel("sftp");
      channel.connect();
      channelSFTP=(ChannelSftp)channel;

      String destFile = SFTPImpl.destFolder + "/" + SFTPImpl.sourceFile + ".part";

      log.info("Downloading file: " + SFTPImpl.sourceFile + " -- START");
      channelSFTP.get(SFTPImpl.sourceFile,destFile,SFTPImpl.monitor,ChannelSftp.OVERWRITE);      

  } catch (JSchException e) {
   log.error("Error occurred within library", e);
   throw new JSchException(e.getMessage(),e);
  } catch (SftpException e) {
   log.error("Error occurred in SFTP communication. Error ID: " + e.id, e);
   throw new SftpException(e.id,e.getMessage(),e);
  } catch (Exception e) {
   throw new Exception(e.getMessage(),e);
  }finally {
   if (channelSFTP != null && channelSFTP.isConnected()) {
    channelSFTP.quit();
       channelSFTP.disconnect();
       session.disconnect();
   }
  }
 }

Applet 使用 Java Deployment Toolkit 进行部署。小程序部署的 HTML 页面代码片段为:

<script src="http://www.java.com/js/deployJava.js"></script>
  <script>
   var attributes = {code:'com.sftptest.applet.SFTPApplet', archive:'signedsftp.jar,jsch.jar,log4j-1.2.15.jar', width:400, height:400} ; 
         var parameters = {jnlp_href: 'sftpdownload-applet.jnlp'} ; 
         deployJava.runApplet(attributes, parameters, '1.6');
  </script> 

sftpdownload-applet.jnlp 文件:

<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+" codebase="" href="">
    <information>
        <title>SFTP Downloader</title>
        <vendor>local</vendor>
    </information>
    <resources>
        <!-- Application Resources -->
        <j2se version="1.6+"
              href="http://java.sun.com/products/autodl/j2se" />
        <jar href="signedsftpsftp.jar" main="true" />
        <jar href="jsch.jar" />
        <jar href="log4j-1.2.15.jar" />
    </resources>
    <applet-desc 
         name="SFTP Downloader Applet"
         main-class="com.sftptest.applet.SFTPApplet"
         width="400"
         height="400">
     </applet-desc>
     <update check="background"/>
</jnlp>

小程序包含一个文件选择器,用于选择下载位置。一旦选择了下载位置,小程序就应该开始下载文件。但一段时间后,控制台中出现以下错误:

[Oct 12 20:39:16] ERROR (SFTPImpl.java:130) - Error occurred within library
com.jcraft.jsch.JSchException: java.security.AccessControlException: access denied (java.net.SocketPermission sftpcal.cognizant.com resolve)
 at com.jcraft.jsch.Util.createSocket(Util.java:341)
 at com.jcraft.jsch.Session.connect(Session.java:182)
 at com.jcraft.jsch.Session.connect(Session.java:150)
 at com.sftptest.SFTPImpl.downloadFile(SFTPImpl.java:111)
 at com.sftptest.ui.DownloadPanel$DownloadTask.doInBackground(DownloadPanel.java:316)
 at com.sftptest.ui.DownloadPanel$DownloadTask.doInBackground(DownloadPanel.java:1)
 at javax.swing.SwingWorker$1.call(SwingWorker.java:278)
 at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
 at java.util.concurrent.FutureTask.run(FutureTask.java:138)
 at javax.swing.SwingWorker.run(SwingWorker.java:317)
 at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
 at java.lang.Thread.run(Thread.java:637)

从异常日志中我看到异常来自 JSch 框架的 Util.createSocket() 方法:

  static Socket createSocket(String host, int port, int timeout) throws JSchException{
    Socket socket=null;
    if(timeout==0){
      try{
        socket=new Socket(host, port);
        return socket;
      }
      catch(Exception e){
        String message=e.toString();
        if(e instanceof Throwable)
          throw new JSchException(message, (Throwable)e);
        throw new JSchException(message);
      }
    }

Please help &如果需要更多信息,请告诉我。

The applet will download file from a SFTP server. JSch libraries are used to create a session, connect to the SFTP server using it, create a SFTP channel and execute GET command for that file on that server. The applet is signed.

Code snippet for downloading a file:

    public static void prepareSession() throws JSchException {

  try {
   session = jsch.getSession(user,host,port);
   session.setConfig("StrictHostKeyChecking", "no");        
      session.setPassword(password);
  } catch (JSchException e) {
   e.printStackTrace();
   throw new JSchException(e.getLocalizedMessage(),e);
  }
 }

 public synchronized static void downloadFile() throws Exception {
  ChannelSftp channelSFTP = null;
  try {
   if (!session.isConnected()) {
    session.connect();
   }
   Channel channel=session.openChannel("sftp");
      channel.connect();
      channelSFTP=(ChannelSftp)channel;

      String destFile = SFTPImpl.destFolder + "/" + SFTPImpl.sourceFile + ".part";

      log.info("Downloading file: " + SFTPImpl.sourceFile + " -- START");
      channelSFTP.get(SFTPImpl.sourceFile,destFile,SFTPImpl.monitor,ChannelSftp.OVERWRITE);      

  } catch (JSchException e) {
   log.error("Error occurred within library", e);
   throw new JSchException(e.getMessage(),e);
  } catch (SftpException e) {
   log.error("Error occurred in SFTP communication. Error ID: " + e.id, e);
   throw new SftpException(e.id,e.getMessage(),e);
  } catch (Exception e) {
   throw new Exception(e.getMessage(),e);
  }finally {
   if (channelSFTP != null && channelSFTP.isConnected()) {
    channelSFTP.quit();
       channelSFTP.disconnect();
       session.disconnect();
   }
  }
 }

Applet is deployed using Java Deployment Toolkit. The HTML page code snippet for applet deployment is:

<script src="http://www.java.com/js/deployJava.js"></script>
  <script>
   var attributes = {code:'com.sftptest.applet.SFTPApplet', archive:'signedsftp.jar,jsch.jar,log4j-1.2.15.jar', width:400, height:400} ; 
         var parameters = {jnlp_href: 'sftpdownload-applet.jnlp'} ; 
         deployJava.runApplet(attributes, parameters, '1.6');
  </script> 

sftpdownload-applet.jnlp file:

<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+" codebase="" href="">
    <information>
        <title>SFTP Downloader</title>
        <vendor>local</vendor>
    </information>
    <resources>
        <!-- Application Resources -->
        <j2se version="1.6+"
              href="http://java.sun.com/products/autodl/j2se" />
        <jar href="signedsftpsftp.jar" main="true" />
        <jar href="jsch.jar" />
        <jar href="log4j-1.2.15.jar" />
    </resources>
    <applet-desc 
         name="SFTP Downloader Applet"
         main-class="com.sftptest.applet.SFTPApplet"
         width="400"
         height="400">
     </applet-desc>
     <update check="background"/>
</jnlp>

The applet contains a file chooser which is used to select the download location. As soon as the download location is chosen, the applet should start downloading the file. But after some time following error is coming in the console:

[Oct 12 20:39:16] ERROR (SFTPImpl.java:130) - Error occurred within library
com.jcraft.jsch.JSchException: java.security.AccessControlException: access denied (java.net.SocketPermission sftpcal.cognizant.com resolve)
 at com.jcraft.jsch.Util.createSocket(Util.java:341)
 at com.jcraft.jsch.Session.connect(Session.java:182)
 at com.jcraft.jsch.Session.connect(Session.java:150)
 at com.sftptest.SFTPImpl.downloadFile(SFTPImpl.java:111)
 at com.sftptest.ui.DownloadPanel$DownloadTask.doInBackground(DownloadPanel.java:316)
 at com.sftptest.ui.DownloadPanel$DownloadTask.doInBackground(DownloadPanel.java:1)
 at javax.swing.SwingWorker$1.call(SwingWorker.java:278)
 at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
 at java.util.concurrent.FutureTask.run(FutureTask.java:138)
 at javax.swing.SwingWorker.run(SwingWorker.java:317)
 at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
 at java.lang.Thread.run(Thread.java:637)

From the exception log I saw that the exception is coming from the JSch framework's Util.createSocket() method:

  static Socket createSocket(String host, int port, int timeout) throws JSchException{
    Socket socket=null;
    if(timeout==0){
      try{
        socket=new Socket(host, port);
        return socket;
      }
      catch(Exception e){
        String message=e.toString();
        if(e instanceof Throwable)
          throw new JSchException(message, (Throwable)e);
        throw new JSchException(message);
      }
    }

Please help & let me know if more information is required.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉态萌生 2024-08-14 17:07:55

是的,您已经签署了小程序,但是您忘记了在 jnlp.json 中请求套接字创建权限。

<security>
  <j2ee-application-client-permissions/>
</security>

You have signed the applet, yes, but you've forgotten to request socket-creation permissions in your jnlp.

<security>
  <j2ee-application-client-permissions/>
</security>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文