You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.6 KiB

1 month ago
package com.example.sshd.config;
import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.lang3.StringUtils;
import org.apache.sshd.SshServer;
import org.apache.sshd.server.PasswordAuthenticator;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
import org.apache.sshd.server.session.ServerSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
1 month ago
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SshConfig {
private static final Logger logger = LoggerFactory.getLogger(SshConfig.class);
1 month ago
@Value("${ssh-server.port}")
private int port;
@Value("${ssh-server.private-key.location}")
private String pkLocation;
@Value("${ssh-server.root.username:root}")
private String rootUsername;
@Bean
public SshServer sshd() throws IOException, NoSuchAlgorithmException {
SshServer sshd = SshServer.setUpDefaultServer();
sshd.setPort(port);
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File(pkLocation).getPath(), "RSA", 2048));
sshd.setPasswordAuthenticator(new PasswordAuthenticator() {
@Override
public boolean authenticate(final String username, final String password, final ServerSession session) {
logger.info("Login Attempt: username = {}, password = {}", username, password);
1 month ago
return StringUtils.equals(username, rootUsername);
}
});
sshd.setShellFactory(new EchoShellFactory());
sshd.start();
return sshd;
}
}