change ftp password to string type

This commit is contained in:
Jörg Prante 2024-04-26 14:36:02 +02:00
parent f189ccc4cf
commit 17feb07e94
5 changed files with 36 additions and 13 deletions

View file

@ -734,8 +734,7 @@ public class FTPEnvironment implements Map<String, Object>, Cloneable {
}
String username = getUsername();
char[] passwordChars = FileSystemProviderSupport.getValue(this, PASSWORD, char[].class, null);
String password = passwordChars != null ? new String(passwordChars) : null;
String password = FileSystemProviderSupport.getValue(this, PASSWORD, String.class, null);
String account = FileSystemProviderSupport.getValue(this, ACCOUNT, String.class, null);
if (account != null) {
if (!client.login(username, password, account)) {

View file

@ -76,8 +76,11 @@ public class SftpFileSystem
private int writeBufferSize;
private final List<FileStore> stores;
public SftpFileSystem(SftpFileSystemProvider provider, String id, ClientSession session,
SftpClientFactory factory, SftpVersionSelector selector) throws IOException {
public SftpFileSystem(SftpFileSystemProvider provider,
String id,
ClientSession session,
SftpClientFactory factory,
SftpVersionSelector selector) throws IOException {
super(provider);
this.id = id;
this.clientSession = Objects.requireNonNull(session, "No client session");

View file

@ -22,6 +22,7 @@ package org.apache.sshd.fs;
import java.io.IOException;
import java.util.Map;
import org.apache.sshd.client.config.hosts.HostConfigEntry;
import org.apache.sshd.client.session.ClientSession;
import org.apache.sshd.client.session.ClientSessionCreator;
import org.apache.sshd.common.auth.PasswordHolder;
@ -50,13 +51,28 @@ public interface SftpFileSystemClientSessionInitializer {
* @return The created {@link ClientSession}
* @throws IOException If failed to connect
*/
default ClientSession createClientSession(
SftpFileSystemProvider provider, SftpFileSystemInitializationContext context)
default ClientSession createClientSession(SftpFileSystemProvider provider,
SftpFileSystemInitializationContext context)
throws IOException {
UsernameHolder user = context.getCredentials();
ClientSessionCreator client = provider.getClientInstance();
// proxy?
String proxy = (String) context.getEnvironment().get("proxy");
if (proxy != null) {
HostConfigEntry entry = new HostConfigEntry();
entry.setProxyJump(proxy);
entry.setUsername(user.getUsername());
entry.setHostName(context.getHost());
entry.setHost(context.getHost());
entry.setPort(context.getPort());
return client.connect(entry)
.verifyDuration(context.getMaxConnectTime())
.getSession();
} else {
return client.connect(user.getUsername(), context.getHost(), context.getPort())
.verifyDuration(context.getMaxConnectTime()).getSession();
.verifyDuration(context.getMaxConnectTime())
.getSession();
}
}
/**
@ -90,8 +106,9 @@ public interface SftpFileSystemClientSessionInitializer {
* @return The created {@link SftpFileSystem}
* @throws IOException If failed to create the file-system
*/
default SftpFileSystem createSftpFileSystem(
SftpFileSystemProvider provider, SftpFileSystemInitializationContext context, ClientSession session,
default SftpFileSystem createSftpFileSystem(SftpFileSystemProvider provider,
SftpFileSystemInitializationContext context,
ClientSession session,
SftpVersionSelector selector)
throws IOException {
return new SftpFileSystem(provider, context.getId(), session, provider.getSftpClientFactory(), selector);

View file

@ -155,7 +155,9 @@ public class SftpFileSystemProvider extends FileSystemProvider {
this(client, null, selector);
}
public SftpFileSystemProvider(SshClient client, SftpClientFactory factory, SftpVersionSelector selector) {
public SftpFileSystemProvider(SshClient client,
SftpClientFactory factory,
SftpVersionSelector selector) {
this.factory = factory;
this.versionSelector = selector;
if (client == null) {

View file

@ -3,6 +3,8 @@ package org.apache.sshd.fs.test;
import java.nio.file.Files;
import org.apache.sshd.client.ClientBuilder;
import org.apache.sshd.client.SshClient;
import org.apache.sshd.client.config.hosts.HostConfigEntry;
import org.apache.sshd.client.session.ClientSession;
import org.apache.sshd.fs.SftpFileSystem;
import org.apache.sshd.fs.SftpFileSystemProvider;
import org.junit.jupiter.api.Test;
@ -20,7 +22,7 @@ public class SFTPFileSystemTest {
private static final Logger logger = Logger.getLogger(SFTPFileSystemTest.class.getName());
@Test
public void init() throws Exception {
public void testXbib() throws Exception {
Map<String, String> env = new HashMap<>();
env.put("username", "joerg");
URI uri = URI.create("sftp://xbib.org");