apache ftpserver搭建ftp服务器


Posted in Servers onMay 20, 2022

操作环境:

  • win2012r2 x64 datacenter
  • Apache FtpServer 1.2.0
  • Java SE Development Kit 8u333
  • commons-dbcp2-2.9.0.jar
  • commons-pool2-2.11.1.jar
  • mysql server 8.0.29
  • mysql-connector-java-8.0.29.jar
  • sqlite
  • sqlite-jdbc-3.36.0.3.jar

如下图:

apache ftpserver搭建ftp服务器

一、usermanager采用文件形式管理xml示例如下

<?xml version="1.0" encoding="UTF-8"?>
  <!--
    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements. See the NOTICE file distributed with
    this work for additional information regarding copyright ownership.
    The ASF licenses this file to you under the Apache License, Version
    2.0 (the "License"); you may not use this file except in compliance
    with the License. You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0 Unless required by
    applicable law or agreed to in writing, software distributed under the
    License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    CONDITIONS OF ANY KIND, either express or implied. See the License for
    the specific language governing permissions and limitations under the
    License.
  -->
<server xmlns="http://mina.apache.org/ftpserver/spring/v1"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="
     http://mina.apache.org/ftpserver/spring/v1 https://mina.apache.org/ftpserver-project/ftpserver-1.0.xsd  
     "
  id="myServer">
  <listeners>
    <nio-listener name="default" port="21">
        <ssl>
                <keystore file="./res/ftpserver.jks" password="password" />
            </ssl>
    </nio-listener>
  </listeners>
  <file-user-manager file="./res/conf/users.properties" />
</server>

二、usermanager采用mysql数据库管理用户时,ftpd-mysql.xml示例如下

目前数据库管理用户时采用的明文存储,salted和md5的方式没有测试成功,如有测试成功的朋友请指导一下。

<?xml version="1.0" encoding="UTF-8"?>
<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor 
    license agreements. See the NOTICE file distributed with this work for additional 
    information regarding copyright ownership. The ASF licenses this file to 
    you under the Apache License, Version 2.0 (the "License"); you may not use 
    this file except in compliance with the License. You may obtain a copy of 
    the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required 
    by applicable law or agreed to in writing, software distributed under the 
    License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 
    OF ANY KIND, either express or implied. See the License for the specific 
    language governing permissions and limitations under the License. -->
<server xmlns="http://mina.apache.org/ftpserver/spring/v1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://mina.apache.org/ftpserver/spring/v1
           http://mina.apache.org/ftpserver/ftpserver-1.0.xsd
           "
    id="myServer">
    <listeners>
        <nio-listener name="default" port="21">
            <ssl>
                <keystore file="./res/ftpserver.jks" password="password" />
            </ssl>
        </nio-listener>
    </listeners>
    <db-user-manager encrypt-passwords="clear">
        <data-source>
            <beans:bean class="org.apache.commons.dbcp2.BasicDataSource">
                <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
                <beans:property name="url" value="jdbc:mysql://localhost/ftpserver" />
                <beans:property name="username" value="root" />
                <beans:property name="password" value="123456" />
            </beans:bean>
        </data-source>
        <insert-user>INSERT INTO FTP_USER (userid, userpassword,
            homedirectory, enableflag, writepermission, idletime, uploadrate,
            downloadrate) VALUES ('{userid}', '{userpassword}',
            '{homedirectory}',
            {enableflag}, {writepermission}, {idletime},
            {uploadrate},
            {downloadrate})
        </insert-user>
        <update-user>UPDATE FTP_USER SET
            userpassword='{userpassword}',homedirectory='{homedirectory}',enableflag={enableflag},writepermission={writepermission},idletime={idletime},uploadrate={uploadrate},downloadrate={downloadrate}
            WHERE userid='{userid}'
        </update-user>
        <delete-user>DELETE FROM FTP_USER WHERE userid = '{userid}'
        </delete-user>
        <select-user>SELECT userid, userpassword, homedirectory,
            enableflag, writepermission, idletime, uploadrate, downloadrate,
            maxloginnumber, maxloginperip FROM
            FTP_USER WHERE userid = '{userid}'
        </select-user>
        <select-all-users>
            SELECT userid FROM FTP_USER ORDER BY userid
        </select-all-users>
        <is-admin>SELECT userid FROM FTP_USER WHERE userid='{userid}'
            AND
            userid='admin'
        </is-admin>
        <authenticate>SELECT userpassword from FTP_USER WHERE
            userid='{userid}'
        </authenticate>
    </db-user-manager>
</server>

注意:org.apache.commons.dbcp2.BasicDataSource,看最新的commons.dbcp命名空间和1.x版本有区别

三、usermanager采用Sqlite数据库管理用户时,ftpd-sqlite.xml示例如下

<?xml version="1.0" encoding="UTF-8"?>
<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor 
    license agreements. See the NOTICE file distributed with this work for additional 
    information regarding copyright ownership. The ASF licenses this file to 
    you under the Apache License, Version 2.0 (the "License"); you may not use 
    this file except in compliance with the License. You may obtain a copy of 
    the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required 
    by applicable law or agreed to in writing, software distributed under the 
    License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 
    OF ANY KIND, either express or implied. See the License for the specific 
    language governing permissions and limitations under the License. -->
<server xmlns="http://mina.apache.org/ftpserver/spring/v1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://mina.apache.org/ftpserver/spring/v1
           http://mina.apache.org/ftpserver/ftpserver-1.0.xsd
           "
    id="myServer">
    <listeners>
        <nio-listener name="default" port="21">
            <ssl>
                <keystore file="./res/ftpserver.jks" password="password" />
            </ssl>
        </nio-listener>
    </listeners>
    <db-user-manager encrypt-passwords="clear">
        <data-source>
            <beans:bean class="org.apache.commons.dbcp2.BasicDataSource">
                <beans:property name="driverClassName" value="org.sqlite.JDBC" />
                <beans:property name="url" value="jdbc:sqlite:ftp.db" />
            </beans:bean>
        </data-source>
        <insert-user>INSERT INTO FTP_USER (userid, userpassword,
            homedirectory, enableflag, writepermission, idletime, uploadrate,
            downloadrate) VALUES ('{userid}', '{userpassword}',
            '{homedirectory}',
            {enableflag}, {writepermission}, {idletime},
            {uploadrate},
            {downloadrate})
        </insert-user>
        <update-user>UPDATE FTP_USER SET
            userpassword='{userpassword}',homedirectory='{homedirectory}',enableflag={enableflag},writepermission={writepermission},idletime={idletime},uploadrate={uploadrate},downloadrate={downloadrate}
            WHERE userid='{userid}'
        </update-user>
        <delete-user>DELETE FROM FTP_USER WHERE userid = '{userid}'
        </delete-user>
        <select-user>SELECT userid, userpassword, homedirectory,
            enableflag, writepermission, idletime, uploadrate, downloadrate,
            maxloginnumber, maxloginperip FROM
            FTP_USER WHERE userid = '{userid}'
        </select-user>
        <select-all-users>
            SELECT userid FROM FTP_USER ORDER BY userid
        </select-all-users>
        <is-admin>SELECT userid FROM FTP_USER WHERE userid='{userid}'
            AND
            userid='admin'
        </is-admin>
        <authenticate>SELECT userpassword from FTP_USER WHERE
            userid='{userid}'
        </authenticate>
    </db-user-manager>
</server>

注意:commons的jar包还保留着,多了个操作sqlitejdbc的jar包,下载地址:GitHub - xerial/sqlite-jdbc: SQLite JDBC Driver

四、解决ftpd.exe在64位windows系统启动失败的问题

需下载tomcat包,目前测试的这个版本可行tomcat-7 v7.0.109 (apache.org)

apache ftpserver搭建ftp服务器

放入apache ftpserver bin目录里替换原有的ftpd.exe

这样安装为服务的时候就可以正常启动了

五、python操作sqlite的ftp.db管理(增加删除)用户

自己搞了个python脚本,采用了sqlalchemy来操作数据库

from sqlalchemy import create_engine
from sqlalchemy import MetaData,Table,Column,Boolean,Integer,String
import os

engine=create_engine('sqlite:///ftp.db')
conn=engine.connect()

metadata=MetaData()

ftpusers=Table('FTP_USER',metadata,
    Column('userid',String(64),primary_key=True),
    Column('userpassword',String(64),nullable=False),
    Column('homedirectory',String(128),nullable=False),
    Column('enableflag',Boolean(),default=True),
    Column('writepermission',Boolean(),default=True),
    Column('idletime',Integer(),default=0),
    Column('uploadrate',Integer(),default=0),
    Column('downloadrate',Integer(),default=0),
    Column('maxloginnumber',Integer(),default=0),
    Column('maxloginperip',Integer(),default=0)
)

metadata.create_all(engine)

def addgeneraluser():

	deluser = ftpusers.delete().where(ftpusers.c.userid=="nic")
	rs = conn.execute(deluser)

	dirname="./files/alluser"
	if not os.path.exists(dirname):
		os.mkdir(dirname)

	ins=ftpusers.insert().values(
		userid="nic",
		userpassword="123321",
		homedirectory=dirname,
		writepermission=0,
		maxloginnumber=1
	)

	result=conn.execute(ins)

def addadmin():
	deladmin = ftpusers.delete().where(ftpusers.c.userid=="admin")
	rs = conn.execute(deladmin)

	ins=ftpusers.insert().values(
		userid="admin",
		userpassword="123456",
		homedirectory="./files",
		writepermission=1
	)

	result=conn.execute(ins)

def getusers():
	sel=ftpusers.select()
	rs=conn.execute(sel)
	print(rs.fetchall())
	
addgeneraluser()
getusers()

可以方便的增加用户了,generaluser只读权限只能同时登录一个,admin权限可读写,不限制。

到此这篇关于pache ftpserver搭建ftp服务器的方法步骤的文章就介绍到这了!


Tags in this post...

Servers 相关文章推荐
Nginx反向代理多个服务器的实现方法
Mar 31 Servers
nginx优化的六点方法
Mar 31 Servers
Nginx tp3.2.3 404问题解决方案
Mar 31 Servers
教你利用Nginx 服务搭建子域环境提升二维地图加载性能的步骤
Sep 25 Servers
Nginx反向代理学习实例教程
Oct 24 Servers
配置Kubernetes外网访问集群
Mar 31 Servers
微信告警的zabbix监控系统 监控整个NGINX集群
Apr 18 Servers
Windows Server 2019 域控制器安装图文教程
Apr 28 Servers
Nginx利用Logrotate实现日志分割
May 20 Servers
Nginx使用ngx_http_upstream_module实现负载均衡功能示例
Aug 05 Servers
Tomcat安装使用及部署Web项目的3种方法汇总
Aug 14 Servers
服务器间如何实现文件共享
May 20 #Servers
Nginx限流和黑名单配置
May 20 #Servers
Nginx利用Logrotate实现日志分割
May 20 #Servers
nginx lua 操作 mysql
May 15 #Servers
Nginx HTTP跳转至HTTPS
Nginx 匹配方式
May 15 #Servers
nginx实现多geoserver服务的负载均衡
May 15 #Servers
You might like
CI框架源码阅读,系统常量文件constants.php的配置
2013/02/28 PHP
PHP中error_reporting()用法详解
2015/08/31 PHP
php将数组存储为文本文件方法汇总
2015/10/28 PHP
PHP切割整数工具类似微信红包金额分配的思路详解
2019/09/18 PHP
使用tp框架和SQL语句查询数据表中的某字段包含某值
2019/10/18 PHP
Dojo 学习要点
2010/09/03 Javascript
js制作的鼠标悬浮时产生的下拉框效果
2012/10/27 Javascript
浅析jQuery中常用的元素查找方法总结
2013/07/04 Javascript
JavaScript中Window对象的属性及事件
2015/12/25 Javascript
JavaScript之生成器_动力节点Java学院整理
2017/06/30 Javascript
angularjs实现对表单输入改变的监控(ng-change和watch两种方式)
2018/08/29 Javascript
javascript中call,apply,callee,caller用法实例分析
2019/07/24 Javascript
vue 组件销毁并重置的实现
2020/01/13 Javascript
Vue的双向数据绑定实现原理解析
2020/02/17 Javascript
js cavans实现静态滚动弹幕
2020/05/21 Javascript
JavaScript undefined及null区别实例解析
2020/07/21 Javascript
总结Python中逻辑运算符的使用
2015/05/13 Python
Python爬取网易云音乐热门评论
2017/03/31 Python
Python简单生成8位随机密码的方法
2017/05/24 Python
python绘制简单折线图代码示例
2017/12/19 Python
Python内置函数reversed()用法分析
2018/03/20 Python
一条命令解决mac版本python IDLE不能输入中文问题
2018/05/15 Python
Python3连接SQLServer、Oracle、MySql的方法
2018/06/28 Python
python之cv2与图像的载入、显示和保存实例
2018/12/05 Python
浅谈python新式类和旧式类区别
2019/04/26 Python
pyinstaller打包单文件时--uac-admin选项不起作用怎么办
2020/04/15 Python
在Anaconda3下使用清华镜像源安装TensorFlow(CPU版)
2020/04/19 Python
营销与策划应届生求职信
2013/11/04 职场文书
员工安全责任书范本
2014/07/24 职场文书
党员自我评议对照检查材料
2014/09/27 职场文书
2015年信息中心工作总结
2015/05/25 职场文书
丧事主持词
2015/07/02 职场文书
被委托人身份证明
2015/08/07 职场文书
幼儿园开学家长寄语(2016春季)
2015/12/03 职场文书
2016年社区植树节活动总结
2016/03/16 职场文书
MySQL 服务和数据库管理
2021/11/11 MySQL