Apache Ftp Server 增加SSL认证

package examples.ftpServer;
import java.io.File;
import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.ssl.SslConfigurationFactory;
import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
public class StartFTPS
{
 /**
  * 通过程序启动FTP with SSL认证,以Apache FTPServer为例
  * @param args
  * @throws FtpException
  */
 public static void main(String[] args) throws FtpException
 {
  // TODO Auto-generated method stub
  FtpServerFactory serverFactory = new FtpServerFactory();
       
  ListenerFactory factory = new ListenerFactory();
         
  // set the port of the listener
  factory.setPort(2221);
  // define SSL configuration
  SslConfigurationFactory ssl = new SslConfigurationFactory();
  ssl.setKeystoreFile(new File("F:/FTP/ftpserver-1.0.5/apache-ftpserver-1.0.5/res/kserver.keystore"));
  ssl.setKeystorePassword("123456");
  
//  ssl.setTruststoreFile(new File("F:/FTP/ftpserver-1.0.5/apache-ftpserver-1.0.5/res/tserver.keystore"));
//  ssl.setKeystorePassword("123456");

  // set the SSL configuration for the listener
  factory.setSslConfiguration(ssl.createSslConfiguration());
  factory.setImplicitSsl(true);
  
  
  // replace the default listener
  serverFactory.addListener("default", factory.createListener());
         
  PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
  userManagerFactory.setFile(new File("F:/FTP/ftpserver-1.0.5/apache-ftpserver-1.0.5/res/conf/users.properties"));
         
  serverFactory.setUserManager(userManagerFactory.createUserManager());
         
  // start the server
  FtpServer server = serverFactory.createServer();
         
  server.start();
 }
}
package examples.ftpClient;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.KeyStore;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTPSClient;
public class ConnectFTPS
{
 private static FTPSClient ftpsClient;
 private static final String trust_path = "F:/FTP/ftpserver-1.0.5/apache-ftpserver-1.0.5/res/kclient.keystore";
 private static final String trust_pw = "123456";
 private static final String key_path = "F:/FTP/ftpserver-1.0.5/apache-ftpserver-1.0.5/res/tclient.keystore";
 private static final String key_pw = "123456";
 private static final String serverIP = "127.0.0.1";
 private static final int serverPort = 2221;
 private static final int defaultTimeout = 10000;
 private static final int soTimeout = 900000;
 private static final int dataTimeout = 5000;
 /**
  * 测试连接FTP With SSL,以Apache FTPServer为例
  * @param args
  * @throws Exception
  */
 public static void main(String[] args) throws Exception
 {
  if (!connect("active"))
  {
   connect("passive");
  }
  FileInputStream fs = new FileInputStream(trust_path);
  System.out.println("storeFile: " + ftpsClient.storeFile("test_file", fs));
  fs.close();
  ftpsClient.disconnect();
 }
  /**
   * 登陆FTP
   * @param active
   * @return
   * @throws Exception
   */
 private static boolean connect(String active) throws Exception
 {
  ftpsClient = new FTPSClient(true);
  ftpsClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
  ftpsClient.setKeyManager(getKeyManager());
  //ftpsClient.setTrustManager(getTrustManager());
  ftpsClient.setDefaultTimeout(defaultTimeout);
  ftpsClient.connect(serverIP, serverPort);
  System.out.println("已经连接FTP");
  ftpsClient.setSoTimeout(soTimeout);
  ftpsClient.getReplyCode();
  ftpsClient.execPBSZ(0);
  ftpsClient.execPROT("P");
  ftpsClient.login("admin", "admin");
  ftpsClient.changeWorkingDirectory("/");
  ftpsClient.setDataTimeout(dataTimeout);
  if (active.equalsIgnoreCase("active"))
  {
   ftpsClient.enterLocalActiveMode();
  } else
  {
   ftpsClient.enterLocalPassiveMode();
  }
  
  System.out.println("已经登陆FTP");
  return testLink();
 }
 /**
  * 遍历FTP文件
  * @return
  */
 private static boolean testLink()
 {
  long t1 = System.currentTimeMillis();
  try
  {
   System.out.println("List file length:" + ftpsClient.listFiles().length);
  } catch (IOException e)
  {
   System.out.println(e.getMessage());
   long t2 = System.currentTimeMillis();
   long t = (t2 - t1) / 1000;
   System.out.println("t: " + t);
   try
   {
    ftpsClient.disconnect();
   } catch (IOException e1)
   {
    e1.printStackTrace();
   }
   return false;
  }
  return true;
 }
 private static KeyManager getKeyManager() throws Exception
 {
  KeyStore key_ks = KeyStore.getInstance("JKS");
  key_ks.load(new FileInputStream(key_path), key_pw.toCharArray());
  KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
  kmf.init(key_ks, key_pw.toCharArray());
  KeyManager[] km = kmf.getKeyManagers();
  System.out.println("km len: " + km.length);
  return km[0];
 }
 private static TrustManager getTrustManager() throws Exception
 {
  KeyStore trust_ks = KeyStore.getInstance("JKS");
  trust_ks.load(new FileInputStream(trust_path), trust_pw.toCharArray());
  TrustManagerFactory tf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
  tf.init(trust_ks);
  TrustManager[] tm = tf.getTrustManagers();
  System.out.println("tm len: " + tm.length);
  return tm[0];
 }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值