这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions libraries-io/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@
<artifactId>jakarta.mail-api</artifactId>
<version>${jakarta-mail.version}</version>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-junit-jupiter</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -80,15 +93,16 @@

<properties>
<!-- sftp -->
<jsch.version>0.2.16</jsch.version>
<jsch.version>2.27.5</jsch.version>
<sshj.version>0.38.0</sshj.version>
<commons-vfs2.version>2.9.0</commons-vfs2.version>
<commons-vfs2.version>2.10.0</commons-vfs2.version>
<zip4j.version>2.11.5</zip4j.version>
<opencsv.version>5.9</opencsv.version>
<spring.version>6.1.4</spring.version>
<simplejavamail.version>8.7.0</simplejavamail.version>
<jakarta-mail.version>2.1.3</jakarta-mail.version>
<imap.version>2.0.1</imap.version>
<testcontainers.version>2.0.1</testcontainers.version>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.baeldung.java.io.jsch;

import com.jcraft.jsch.UserInfo;

public class BaseUserInfo implements UserInfo {
@Override
public String getPassphrase() {
return null;
}

@Override
public String getPassword() {
return null;
}

@Override
public boolean promptPassword(String message) {
return false;
}

@Override
public boolean promptPassphrase(String message) {
return false;
}

@Override
public boolean promptYesNo(String message) {
return false;
}

@Override
public void showMessage(String message) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package com.baeldung.java.io.jsch;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.nio.file.Paths;

import org.junit.jupiter.api.Test;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.MountableFile;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.JSchUnknownHostKeyException;
import com.jcraft.jsch.Session;

@Testcontainers
class ConnectIntegrationTest {
private static final int SFTP_PORT = 22;

private static final String USERNAME = "baeldung";
private static final String PASSWORD = "test";
private static final String DIRECTORY = "upload";

// atmoz/sftp is a Docker container for an SFTP server that can easily support password and key authentication.
@Container
public static GenericContainer<?> sftpContainer = new GenericContainer<>("atmoz/sftp:latest")
.withExposedPorts(SFTP_PORT)
.withCommand(USERNAME + ":" + PASSWORD + ":::" + DIRECTORY)
.withCopyFileToContainer(
MountableFile.forHostPath(Paths.get("src/test/resources/com/baeldung/java/io/jsch/nopassphrase/id_rsa.pub")),
"/home/" + USERNAME + "/.ssh/keys/id_rsa.pub"
)
.withCopyFileToContainer(
MountableFile.forHostPath(Paths.get("src/test/resources/com/baeldung/java/io/jsch/passphrase/id_rsa.pub")),
"/home/" + USERNAME + "/.ssh/keys/id_rsa2.pub"
);

@Test
void whenTheHostKeyIsUnknown_thenTheConnectionFails() throws Exception {
JSch jSch = new JSch();

Session session = jSch.getSession(USERNAME, sftpContainer.getHost(), sftpContainer.getMappedPort(SFTP_PORT));

session.setUserInfo(new BaseUserInfo() {});

assertThrows(JSchUnknownHostKeyException.class, () -> session.connect());
}

@Test
void whenNoAuthenticationIsProvided_thenTheConnectionFails() throws Exception {
JSch jSch = new JSch();

Session session = jSch.getSession(USERNAME, sftpContainer.getHost(), sftpContainer.getMappedPort(SFTP_PORT));

session.setUserInfo(new BaseUserInfo() {

@Override
public boolean promptYesNo(String message) {
if (message.startsWith("The authenticity of host")) {
return true;
}

return false;
}
});

JSchException exception = assertThrows(JSchException.class, () -> session.connect());
assertEquals("Auth cancel for methods 'publickey,password,keyboard-interactive'", exception.getMessage());
}

@Test
void whenPasswordAuthIsProvided_thenTheConnectionSucceeds() throws Exception {
JSch jSch = new JSch();

Session session = jSch.getSession(USERNAME, sftpContainer.getHost(), sftpContainer.getMappedPort(SFTP_PORT));

session.setUserInfo(new BaseUserInfo() {
@Override
public String getPassword() {
return PASSWORD;
}

@Override
public boolean promptPassword(String message) {
return true;
}

@Override
public boolean promptYesNo(String message) {
if (message.startsWith("The authenticity of host")) {
return true;
}

return false;
}
});

session.connect();
}

@Test
void givenAnOpenSSHKey_whenKeyAuthIsProvided_thenTheConnectionSucceeds() throws Exception {
JSch jSch = new JSch();
jSch.addIdentity("src/test/resources/com/baeldung/java/io/jsch/nopassphrase/id_rsa");

Session session = jSch.getSession(USERNAME, sftpContainer.getHost(), sftpContainer.getMappedPort(SFTP_PORT));

session.setUserInfo(new BaseUserInfo() {
@Override
public boolean promptYesNo(String message) {
if (message.startsWith("The authenticity of host")) {
return true;
}

return false;
}
});

session.connect();
}

@Test
void givenAPuttyKey_whenKeyAuthIsProvided_thenTheConnectionSucceeds() throws Exception {
JSch jSch = new JSch();
jSch.addIdentity("src/test/resources/com/baeldung/java/io/jsch/nopassphrase/id_rsa.ppk");

Session session = jSch.getSession(USERNAME, sftpContainer.getHost(), sftpContainer.getMappedPort(SFTP_PORT));

session.setUserInfo(new BaseUserInfo() {
@Override
public boolean promptYesNo(String message) {
if (message.startsWith("The authenticity of host")) {
return true;
}

return false;
}
});

session.connect();
}

@Test
void givenAnOpenSSHKeyWithPassphrase_whenKeyAuthIsProvided_thenTheConnectionSucceeds() throws Exception {
JSch jSch = new JSch();
jSch.addIdentity("src/test/resources/com/baeldung/java/io/jsch/passphrase/id_rsa");

Session session = jSch.getSession(USERNAME, sftpContainer.getHost(), sftpContainer.getMappedPort(SFTP_PORT));

session.setUserInfo(new BaseUserInfo() {
@Override
public String getPassphrase() {
return "te5tPa55word";
}

@Override
public boolean promptPassphrase(String message) {
return true;
}

@Override
public boolean promptYesNo(String message) {
if (message.startsWith("The authenticity of host")) {
return true;
}

return false;
}
});

session.connect();
}
}
Loading