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 + consul + upsync 完成动态负载均衡的方法详解
Mar 31 Servers
关于nginx 实现jira反向代理的问题
Sep 25 Servers
zabbix自定义监控nginx状态实现过程
Nov 01 Servers
idea下配置tomcat避坑详解
Apr 12 Servers
Windows Server 2012 修改远程默认端口3389的方法
Apr 28 Servers
Windows server 2012搭建FTP服务器
Apr 29 Servers
Windows Server 2012 R2 磁盘分区教程
Apr 29 Servers
windows server2008 开启端口的实现方法
Jun 25 Servers
vscode内网访问服务器的方法
Jun 28 Servers
zabbix 代理服务器的部署与 zabbix-snmp 监控问题
Jul 15 Servers
Linux在两个服务器直接传文件的操作方法
Aug 05 Servers
zabbix如何添加监控主机和自定义监控项
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
PHP4实际应用经验篇(2)
2006/10/09 PHP
php自动跳转中英文页面
2008/07/29 PHP
全世界最小的php网页木马一枚 附PHP木马的防范方法
2009/10/09 PHP
php读取mysql乱码,用set names XXX解决的原理分享
2011/12/29 PHP
thinkphp 字母函数详解T/I/N/D/M/A/R/U
2017/04/03 PHP
javascript(jquery)利用函数修改全局变量的代码
2009/11/02 Javascript
一些javascript一些题目的解析
2010/12/25 Javascript
javascript面向对象之二 命名空间
2011/02/08 Javascript
用jquery实现自定义风格的滑动条实现代码
2011/04/26 Javascript
jQuery简单几行代码实现tab切换
2015/03/10 Javascript
详细解读JavaScript编程中的Promise使用
2015/07/27 Javascript
下雪了 javascript实现雪花飞舞
2020/08/02 Javascript
第一次记录Bootstrap table学习笔记(1)
2017/05/18 Javascript
js学习总结之DOM2兼容处理重复问题的解决方法
2017/07/27 Javascript
js禁止浏览器页面后退功能的实例(推荐)
2017/09/01 Javascript
vue2.0路由切换后页面滚动位置不变BUG的解决方法
2018/03/14 Javascript
Nuxt页面级缓存的实现
2020/03/09 Javascript
跟老齐学Python之正规地说一句话
2014/09/28 Python
安装Python的教程-Windows
2017/07/22 Python
django实现前后台交互实例
2017/08/07 Python
Python八大常见排序算法定义、实现及时间消耗效率分析
2018/04/27 Python
Python脚本按照当前日期创建多级目录
2019/03/01 Python
java中的控制结构(if,循环)详解
2019/06/26 Python
python 利用pywifi模块实现连接网络破解wifi密码实时监控网络
2019/09/16 Python
numpy ndarray 取出满足特定条件的某些行实例
2019/12/05 Python
PyCharm2019 安装和配置教程详解附激活码
2020/07/31 Python
基于Python爬取素材网站音频文件
2020/10/21 Python
canvas 基础之图像处理的使用
2020/04/10 HTML / CSS
巴西家用小家电购物网站:Polishop
2016/08/07 全球购物
Cotton On南非:澳洲时尚平价品牌
2018/06/28 全球购物
优秀团员事迹材料1000字
2014/08/20 职场文书
护士医德医风自我评价
2014/09/15 职场文书
无犯罪记录证明样本
2015/06/16 职场文书
2019新员工试用期转正工作总结范文
2019/08/21 职场文书
用Python将库打包发布到pypi
2021/04/13 Python
pytorch中的 .view()函数的用法介绍
2022/03/17 Python