gpt4 book ai didi

java - Apache FTP 服务器 - 以编程方式创建用户

转载 作者:行者123 更新时间:2023-12-02 05:10:04 25 4
gpt4 key购买 nike

我正在使用代码中嵌入的 Apache FTP 服务器。我使用了 Apache 网站上的示例,并且确实在 5 分钟内嵌入了服务器(如图所示)

Apache Example here

此示例使用 users.properties 文件,这很好。

但是,我想在代码中创建我的用户。我不希望用户能够更改属性。

我在网上找到了各种示例,但似乎都不完整,并且似乎没有包含我需要的一切。

简而言之,我想根据以下属性配置在代码中创建一个用户

# Password is "admin"
ftpserver.user.admin.userpassword=21232F297A57A5A743894A0E4A801FC3
ftpserver.user.admin.homedirectory=/home/ftproot
ftpserver.user.admin.enableflag=true
ftpserver.user.admin.writepermission=true
ftpserver.user.admin.maxloginnumber=0
ftpserver.user.admin.maxloginperip=0
ftpserver.user.admin.idletime=0
ftpserver.user.admin.uploadrate=0
ftpserver.user.admin.downloadrate=0

我尝试了一些方法,包括以下方法,但均无济于事:

    UserFactory uf = new UserFactory();

uf.setName( "admin" );
uf.setPassword( "admin" );
uf.setHomeDirectory( "/home/ftproot" );
uf.setMaxIdleTime( 0 );
uf.setEnabled(true);
uf.createUser();

我遗漏了一些东西,并且无法在网络上找到任何完整/有效的示例。

编辑

这是我收到的错误消息

C:\WINDOWS>ftp localhost
Connected to localhost.
220 Service ready for new user.
User (localhost:(none)): admin
331 User name okay, need password for admin.
Password:
530 Authentication failed.
Login failed.
ftp>

最佳答案

FtpServerFactory serverFactory = new FtpServerFactory();
ListenerFactory factory = new ListenerFactory();
//factory.setServerAddress("127.0.0.1");

// set the port of the listener
factory.setPort(3232);
factory.setIdleTimeout(3000);

// replace the default listener
serverFactory.addListener("default", factory.createListener());

System.out.println("Adding Users Now");
PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
userManagerFactory.setFile(new File("users.properties"));

userManagerFactory.setPasswordEncryptor(new PasswordEncryptor()
{//We store clear-text passwords in this example

@Override
public String encrypt(String password) {
return password;
}

@Override
public boolean matches(String passwordToCheck, String storedPassword) {
return passwordToCheck.equals(storedPassword);
}
});

BaseUser user1 = new BaseUser();
user1.setName("test");
user1.setPassword("test");
user1.setHomeDirectory("D:/Softwares/ApacheFTP/ftpserver-1.0.6/apache-ftpserver-1.0.6/res/home");
List<Authority> authorities = new ArrayList<Authority>();
authorities.add(new WritePermission());
user1.setAuthorities(authorities);
UserManager um = userManagerFactory.createUserManager();
try
{
um.save(user1);//Save the user to the user list on the filesystem
}
catch (FtpException e1)
{
e1.printStackTrace();
}

serverFactory.setUserManager(um);


Map<String, Ftplet> m = new HashMap<String, Ftplet>();
m.put("miaFtplet", new Ftplet()
{

@Override
public void init(FtpletContext ftpletContext) throws FtpException {
System.out.println("init");
//System.out.println("Thread #" + Thread.currentThread().getId());
}

@Override
public void destroy() {
System.out.println("destroy");
//System.out.println("Thread #" + Thread.currentThread().getId());
}

@Override
public FtpletResult beforeCommand(FtpSession session, FtpRequest request) throws FtpException, IOException
{
//System.out.println("beforeCommand " + session.getUserArgument() + " : " + session.toString() + " | " + request.getArgument() + " : " + request.getCommand() + " : " + request.getRequestLine());
//System.out.println("Thread #" + Thread.currentThread().getId());

//do something
return FtpletResult.DEFAULT;//...or return accordingly
}

@Override
public FtpletResult afterCommand(FtpSession session, FtpRequest request, FtpReply reply) throws FtpException, IOException
{
//System.out.println("afterCommand " + session.getUserArgument() + " : " + session.toString() + " | " + request.getArgument() + " : " + request.getCommand() + " : " + request.getRequestLine() + " | " + reply.getMessage() + " : " + reply.toString());
//System.out.println("Thread #" + Thread.currentThread().getId());

//do something
return FtpletResult.DEFAULT;//...or return accordingly
}

@Override
public FtpletResult onConnect(FtpSession session) throws FtpException, IOException
{
//System.out.println("onConnect " + session.getUserArgument() + " : " + session.toString());
//System.out.println("Thread #" + Thread.currentThread().getId());

//do something
return FtpletResult.DEFAULT;//...or return accordingly
}

@Override
public FtpletResult onDisconnect(FtpSession session) throws FtpException, IOException
{
//System.out.println("onDisconnect " + session.getUserArgument() + " : " + session.toString());
//System.out.println("Thread #" + Thread.currentThread().getId());

//do something
return FtpletResult.DEFAULT;//...or return accordingly
}
});
serverFactory.setFtplets(m);

// start the server
FtpServer server = serverFactory.createServer();

System.out.println("Server Starting" + factory.getPort());
try{
server.start();
}
catch(Exception e2){e2.printStackTrace();}

关于java - Apache FTP 服务器 - 以编程方式创建用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27404709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com