本文整理汇总了Java中org.apache.sshd.common.session.Session类的典型用法代码示例。如果您正苦于以下问题:Java Session类的具体用法?Java Session怎么用?Java Session使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Session类属于org.apache.sshd.common.session包,在下文中一共展示了Session类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testReExchangeFromJschClient
import org.apache.sshd.common.session.Session; //导入依赖的package包/类
@Test
public void testReExchangeFromJschClient() throws Exception {
Assume.assumeTrue("DH Group Exchange not supported", SecurityUtils.isDHGroupExchangeSupported());
setUp(0L, 0L, 0L);
JSch.setConfig("kex", BuiltinDHFactories.Constants.DIFFIE_HELLMAN_GROUP_EXCHANGE_SHA1);
JSch sch = new JSch();
com.jcraft.jsch.Session s = sch.getSession(getCurrentTestName(), TEST_LOCALHOST, port);
try {
s.setUserInfo(new SimpleUserInfo(getCurrentTestName()));
s.connect();
com.jcraft.jsch.Channel c = s.openChannel(Channel.CHANNEL_SHELL);
c.connect();
try (OutputStream os = c.getOutputStream();
InputStream is = c.getInputStream()) {
String expected = "this is my command\n";
byte[] bytes = expected.getBytes(StandardCharsets.UTF_8);
byte[] data = new byte[bytes.length + Long.SIZE];
for (int i = 1; i <= 10; i++) {
os.write(bytes);
os.flush();
int len = is.read(data);
String str = new String(data, 0, len);
assertEquals("Mismatched data at iteration " + i, expected, str);
outputDebugMessage("Request re-key #%d", i);
s.rekey();
}
} finally {
c.disconnect();
}
} finally {
s.disconnect();
}
}
示例2: createFileSystem
import org.apache.sshd.common.session.Session; //导入依赖的package包/类
@Override
public FileSystem createFileSystem(Session session) {
return new MockFileSystem("mockfs") {
@Override
public Path getPath(String first, String... more) {
Path path = new File(first).toPath().getFileName();
if (path == null) {
throw new IllegalArgumentException("First is not valid");
}
try {
ClassPathResource classPathResource = new ClassPathResource(path.toString());
InputStream inputStream = classPathResource.getInputStream();
File tempFile = path.toFile();
try (OutputStream outputStream = new FileOutputStream(tempFile)) {
IOUtils.copy(inputStream, outputStream);
} catch (IOException e) {
LOGGER.error("can't write " + path, e);
}
return tempFile.toPath();
} catch (IOException ignored) {
LOGGER.info("can't retrieve path from classpath, let's return with a file path from working directory");
return new File(first).toPath().getFileName();
}
}
};
}
示例3: create
import org.apache.sshd.common.session.Session; //导入依赖的package包/类
public static SshServer create() {
SshServer sshd = SshServer.setUpDefaultServer();
sshd.setPort(SpashConfig.getInstance().spashListenPort());
AbstractGeneratorHostKeyProvider keyProvider = new SimpleGeneratorHostKeyProvider(new File(SpashConfig.getInstance().spashKeyFileName()));
keyProvider.setAlgorithm(SpashConfig.getInstance().spashKeyAlgorithm());
keyProvider.setKeySize(SpashConfig.getInstance().spashKeyLength());
sshd.setKeyPairProvider(keyProvider);
List<NamedFactory<UserAuth>> userAuthFactories = new ArrayList<NamedFactory<UserAuth>>();
userAuthFactories.add(new UserAuthPasswordFactory());
sshd.setUserAuthFactories(userAuthFactories);
sshd.setPasswordAuthenticator(new PasswordAuthenticator() {
@Override
public boolean authenticate(String username, String password, ServerSession serverSession) throws PasswordChangeRequiredException {
return username!=null && username.length()>0 && username.equals(password);
}
});
sshd.setShellFactory(new SpashShellFactory());
List<NamedFactory<Command>> namedFactoryList = new ArrayList<>();
namedFactoryList.add(new SftpSubsystemFactory());
sshd.setSubsystemFactories(namedFactoryList);
sshd.setCommandFactory(new ScpCommandFactory());
sshd.setFileSystemFactory(new FileSystemFactory() {
@Override
public FileSystem createFileSystem(Session session) throws IOException {
return SpashFileSystem.get().getFileSystem();
}
});
return sshd;
}
示例4: AsyncUserAuthService
import org.apache.sshd.common.session.Session; //导入依赖的package包/类
public AsyncUserAuthService(Session s) throws SshException {
ValidateUtils.checkTrue(s instanceof ServerSession, "Server side service used on client side");
if (s.isAuthenticated()) {
throw new SshException("Session already authenticated");
}
this.session = (ServerSession) s;
maxAuthRequests = session.getIntProperty(ServerFactoryManager.MAX_AUTH_REQUESTS, DEFAULT_MAX_AUTH_REQUESTS);
ServerFactoryManager manager = getFactoryManager();
userAuthFactories = new ArrayList<>(manager.getUserAuthFactories());
// Get authentication methods
authMethods = new ArrayList<>();
String mths = FactoryManagerUtils.getString(manager, ServerFactoryManager.AUTH_METHODS);
if (GenericUtils.isEmpty(mths)) {
for (NamedFactory<UserAuth> uaf : manager.getUserAuthFactories()) {
authMethods.add(new ArrayList<>(Collections.singletonList(uaf.getName())));
}
}
else {
for (String mthl : mths.split("\\s")) {
authMethods.add(new ArrayList<>(Arrays.asList(mthl.split(","))));
}
}
// Verify all required methods are supported
for (List<String> l : authMethods) {
for (String m : l) {
NamedFactory<UserAuth> factory = NamedResource.Utils.findByName(m, String.CASE_INSENSITIVE_ORDER, userAuthFactories);
if (factory == null) {
throw new SshException("Configured method is not supported: " + m);
}
}
}
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine("Authorized authentication methods: "+ NamedResource.Utils.getNames(userAuthFactories));
}
}
示例5: createConnection
import org.apache.sshd.common.session.Session; //导入依赖的package包/类
@Override
protected TtyCommand createConnection(Consumer<Connection> onConnect) {
return new TtyCommand(charset, onConnect) {
@Override
public void execute(Runnable task) {
Session session = this.session.getSession();
NettyIoSession ioSession = (NettyIoSession) session.getIoSession();
ioSession.execute(task);
}
};
}
示例6: ClientUserAuthServiceOld
import org.apache.sshd.common.session.Session; //导入依赖的package包/类
public ClientUserAuthServiceOld(Session s) {
if (!(s instanceof ClientSessionImpl)) {
throw new IllegalStateException("Client side service used on server side");
}
session = (ClientSessionImpl) s;
lock = session.getLock();
// Maintain the current auth status in the authFuture.
authFuture = new DefaultAuthFuture(lock);
}
示例7: data
import org.apache.sshd.common.session.Session; //导入依赖的package包/类
@Override
public int data(final ChannelSession channel, byte[] buf, int start, int len) throws IOException {
buffer.append(new String(buf, start, len));
for (int i = 0; i < buffer.length(); i++) {
if (buffer.charAt(i) == '\n') {
final String s = buffer.substring(0, i + 1);
final byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
out.write(new ByteArrayBuffer(bytes)).addListener(new SshFutureListener<IoWriteFuture>() {
@Override
public void operationComplete(IoWriteFuture future) {
Session session = channel.getSession();
if (future.isWritten()) {
try {
Window wLocal = channel.getLocalWindow();
wLocal.consumeAndCheck(bytes.length);
} catch (IOException e) {
session.exceptionCaught(e);
}
} else {
Throwable t = future.getException();
session.exceptionCaught(t);
}
}
});
buffer = new StringBuilder(buffer.substring(i + 1));
i = 0;
}
}
return 0;
}
示例8: createConnection
import org.apache.sshd.common.session.Session; //导入依赖的package包/类
@Override
protected TtyCommand createConnection(Consumer<TtyConnection> onConnect) {
return new TtyCommand(charset, onConnect) {
@Override
public void execute(Runnable task) {
Session session = this.session.getSession();
NettyIoSession ioSession = (NettyIoSession) session.getIoSession();
ioSession.execute(task);
}
};
}
示例9: createFileSystem
import org.apache.sshd.common.session.Session; //导入依赖的package包/类
@Override
public FileSystem createFileSystem(final Session session) throws IOException {
final String userName = session.getUsername();
final String home = db.getHome(userName);
if (home == null) {
throw new IOException("user home error");
}
final RootedFileSystemProvider rfsp = db.hasWritePerm(userName) ? new RootedFileSystemProvider()
: new ReadOnlyRootedFileSystemProvider();
return rfsp.newFileSystem(Paths.get(home), Collections.<String, Object>emptyMap());
}
示例10: create
import org.apache.sshd.common.session.Session; //导入依赖的package包/类
@Override
public Service create(Session session) throws IOException {
return new AsyncUserAuthService(session);
}
示例11: AsyncUserAuthService
import org.apache.sshd.common.session.Session; //导入依赖的package包/类
public AsyncUserAuthService(Session s) throws SshException {
ValidateUtils.checkTrue(s instanceof ServerSession, "Server side service used on client side");
if (s.isAuthenticated()) {
throw new SshException("Session already authenticated");
}
serverSession = (ServerSession) s;
maxAuthRequests = PropertyResolverUtils.getIntProperty(s, ServerAuthenticationManager.MAX_AUTH_REQUESTS, ServerAuthenticationManager.DEFAULT_MAX_AUTH_REQUESTS);
List<NamedFactory<UserAuth>> factories = ValidateUtils.checkNotNullAndNotEmpty(
serverSession.getUserAuthFactories(), "No user auth factories for %s", s);
userAuthFactories = new ArrayList<>(factories);
// Get authentication methods
authMethods = new ArrayList<>();
String mths = PropertyResolverUtils.getString(s, ServerFactoryManager.AUTH_METHODS);
if (GenericUtils.isEmpty(mths)) {
for (NamedFactory<UserAuth> uaf : factories) {
authMethods.add(new ArrayList<>(Collections.singletonList(uaf.getName())));
}
} else {
if (log.isDebugEnabled()) {
log.debug("ServerUserAuthService({}) using configured methods={}", s, mths);
}
for (String mthl : mths.split("\\s")) {
authMethods.add(new ArrayList<>(Arrays.asList(GenericUtils.split(mthl, ','))));
}
}
// Verify all required methods are supported
for (List<String> l : authMethods) {
for (String m : l) {
NamedFactory<UserAuth> factory = NamedResource.Utils.findByName(m, String.CASE_INSENSITIVE_ORDER, userAuthFactories);
if (factory == null) {
throw new SshException("Configured method is not supported: " + m);
}
}
}
if (log.isDebugEnabled()) {
log.debug("ServerUserAuthService({}) authorized authentication methods: {}",
s, NamedResource.Utils.getNames(userAuthFactories));
}
}
示例12: create
import org.apache.sshd.common.session.Session; //导入依赖的package包/类
@Override
public Service create(Session session) throws IOException {
return new ClientUserAuthServiceOld(session);
}