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代理如何解决前端跨域问题详析
Apr 02 Servers
nginx配置虚拟主机的详细步骤
Jul 21 Servers
Z-Order加速Hudi大规模数据集方案分析
Mar 31 Servers
Nginx动静分离配置实现与说明
Apr 07 Servers
Windows Server 2008 修改远程登录端口以及配置防火墙
Apr 28 Servers
如何开启Apache,Nginx和IIS服务器的GZIP压缩功能
Apr 29 Servers
项目中Nginx多级代理是如何获取客户端的真实IP地址
May 30 Servers
Linux磁盘管理方法介绍
Jun 01 Servers
V Rising 服务器搭建图文教程
Jun 16 Servers
Nginx开源可视化配置工具NginxConfig使用教程
Jun 21 Servers
windows server2012 R2下安装PaddleOCR服务的的详细步骤
Sep 23 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
Linux下安装oracle客户端并配置php5.3
2014/10/12 PHP
php计算指定目录下文件占用空间的方法
2015/03/13 PHP
php递归调用删除数组空值元素的方法
2015/04/28 PHP
php post大量数据时发现数据丢失问题解决方法
2015/06/20 PHP
Smarty使用自定义资源的方法
2015/08/08 PHP
PHP实现bitmap位图排序与求交集的方法
2016/07/28 PHP
CI框架使用composer安装的依赖包步骤与方法分析
2016/11/21 PHP
laravel5.6实现数值转换
2019/10/23 PHP
鼠标图片振动代码
2006/07/06 Javascript
HTML中的setCapture和releaseCapture使用介绍
2012/03/21 Javascript
js返回上一页并刷新代码整理
2012/12/21 Javascript
JavaScript和CSS通过expression实现Table居中显示
2013/06/28 Javascript
JQuery 获取json数据$.getJSON方法的实例代码
2013/08/02 Javascript
JavaScript 作用域链解析
2014/11/13 Javascript
jQuery实现的简单百分比进度条效果示例
2016/08/01 Javascript
微信小程序  Mustache语法详细介绍
2016/10/27 Javascript
在Vue中使用echarts的实例代码(3种图)
2017/07/10 Javascript
小程序实现页面顶部选项卡效果
2018/11/06 Javascript
element-ui多文件上传的实现示例
2019/04/10 Javascript
基于JS正则表达式实现模板数据动态渲染(实现思路详解)
2020/03/07 Javascript
解决vue动态下拉菜单 有数据未反应的问题
2020/08/06 Javascript
python中ConfigParse模块的用法
2014/09/29 Python
Python脚本判断 Linux 是否运行在虚拟机上
2015/04/25 Python
Python面向对象编程中关于类和方法的学习笔记
2016/06/30 Python
机器学习python实战之手写数字识别
2017/11/01 Python
Python使用matplotlib的pie函数绘制饼状图功能示例
2018/01/08 Python
Python cookbook(数据结构与算法)将多个映射合并为单个映射的方法
2018/04/19 Python
python 自动去除空行的实例
2018/07/24 Python
浅谈Python中eval的强大与危害
2019/03/13 Python
python如何读取bin文件并下发串口
2019/07/05 Python
华为智利官方商店:Huawei Chile
2020/05/09 全球购物
电子商务助理求职自荐信
2014/04/10 职场文书
建筑施工安全生产责任书
2014/07/22 职场文书
小学教育见习报告
2014/10/31 职场文书
2015年学校团委工作总结
2015/05/26 职场文书
2015年国庆节标语大全
2015/07/30 职场文书