本文整理匯總了Java中org.apache.sshd.server.session.ServerSession類的典型用法代碼示例。如果您正苦於以下問題:Java ServerSession類的具體用法?Java ServerSession怎麽用?Java ServerSession使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
ServerSession類屬於org.apache.sshd.server.session包,在下文中一共展示了ServerSession類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: authenticate
import org.apache.sshd.server.session.ServerSession; //導入依賴的package包/類
/**
* {@inheritDoc}
* @see org.apache.sshd.server.PublickeyAuthenticator#authenticate(java.lang.String, java.security.PublicKey, org.apache.sshd.server.session.ServerSession)
*/
@Override
public boolean authenticate(String username, PublicKey key, ServerSession session) {
if(username==null) throw new IllegalArgumentException("The passed user name was null", new Throwable());
if(key==null) throw new IllegalArgumentException("The passed key was null", new Throwable());
if(log.isDebugEnabled()) log.debug("Authenticating [" + username + "]");
Set<PublicKey> userKeys = pks.get(username);
if(userKeys==null) {
synchronized(pks) {
userKeys = pks.get(username);
if(userKeys==null) {
log.info("Authentication failed for [" + username + "]. No authorized keys found.");
return false;
}
}
}
if(userKeys.contains(key)) {
log.info("Authenticated [" + username + "]");
return true;
}
log.info("Authentication failed for [" + username + "]. No authorized keys matched.");
return false;
}
示例2: setupSftp
import org.apache.sshd.server.session.ServerSession; //導入依賴的package包/類
protected void setupSftp(int port, final String username, final String password, File fileSystemFolder)
throws IOException {
sshd = SshServer.setUpDefaultServer();
sshd.setPort(port);
SftpSubsystem.Factory factory = new SftpSubsystem.Factory();
@SuppressWarnings("unchecked")
List<NamedFactory<Command>> factoryList = Arrays.<NamedFactory<Command>> asList(new NamedFactory[] {factory});
this.sshd.setSubsystemFactories(factoryList);
sshd.setPasswordAuthenticator(new PasswordAuthenticator() {
public boolean authenticate(String tryUsername, String tryPassword, ServerSession session) {
return (username.equals(tryUsername)) && (password.equals(tryPassword));
}
});
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());
sshd.start();
VirtualFileSystemFactory vfSysFactory = new VirtualFileSystemFactory();
vfSysFactory.setDefaultHomeDir(fileSystemFolder.getCanonicalPath());
sshd.setFileSystemFactory(vfSysFactory);
LOGGER.info("Embedded SFTP started on port {}", Integer.valueOf(sshd.getPort()));
}
示例3: authenticate
import org.apache.sshd.server.session.ServerSession; //導入依賴的package包/類
@Override
public boolean authenticate(String username, PublicKey publicKey,
ServerSession session) {
if (username == null || publicKey == null) {
return false;
}
SimpleSession shiroSession = new SimpleSession();
shiroSession.setTimeout(-1L);
Subject subject = new Subject.Builder(securityManager)
.session(shiroSession)
.host(session.getIoSession().getRemoteAddress().toString())
.buildSubject();
try {
subject.login(new PublicKeyToken(username, publicKey));
} catch (AuthenticationException e) {
return false;
}
// Store subject in session.
session.setAttribute(ScmSshServer.SUBJECT_SESSION_ATTRIBUTE_KEY,
subject);
return true;
}
示例4: authenticate
import org.apache.sshd.server.session.ServerSession; //導入依賴的package包/類
@Override
public boolean authenticate(String username, String password,
ServerSession session) {
if (username == null || password == null) {
return false;
}
SimpleSession shiroSession = new SimpleSession();
shiroSession.setTimeout(-1L);
Subject subject = new Subject.Builder(securityManager)
.session(shiroSession)
.host(session.getIoSession().getRemoteAddress().toString())
.buildSubject();
try {
subject.login(new UsernamePasswordToken(username, password));
} catch (AuthenticationException e) {
return false;
}
// Store subject in session.
session.setAttribute(ScmSshServer.SUBJECT_SESSION_ATTRIBUTE_KEY,
subject);
return true;
}
示例5: usePasswordAuthentication
import org.apache.sshd.server.session.ServerSession; //導入依賴的package包/類
/**
* Setup a password authentication.
*
* @param login Login for an authentication.
* @param password Password for an authentication.
* @return This instance of builder.
*/
public MockSshServerBuilder usePasswordAuthentication(
final String login, final String password) {
this.factories.add(new UserAuthPasswordFactory());
final PasswordAuthenticator auth =
Mockito.mock(PasswordAuthenticator.class);
Mockito.when(
auth.authenticate(
Mockito.eq(login),
Mockito.eq(password),
Mockito.any(ServerSession.class)
)
).thenReturn(true);
this.pwd = Optional.of(auth);
return this;
}
示例6: startSshServer
import org.apache.sshd.server.session.ServerSession; //導入依賴的package包/類
@BeforeClass
public static void startSshServer() throws IOException {
sshServer = SshServer.setUpDefaultServer();
ServerSocket serverSocket = new ServerSocket(0);
sshPort = serverSocket.getLocalPort();
serverSocket.close();
sshServer.setPort(sshPort);
sshServer.setPasswordAuthenticator(
new PasswordAuthenticator() {
@Override
public boolean authenticate(
String username,
String password,
ServerSession session) {
return "ssh".equals(username) && "secret".equals(password);
}
});
sshServer.setShellFactory(new SshEchoCommandFactory());
sshServer.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());
sshServer.start();
}
示例7: setupSftpServer
import org.apache.sshd.server.session.ServerSession; //導入依賴的package包/類
public void setupSftpServer() throws IOException {
sshd = SshServer.setUpDefaultServer();
sshd.setHost("127.0.0.1");
sshd.setPort(4922);
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("target/hostkey.ser"));
sshd.setPasswordAuthenticator(new PasswordAuthenticator() {
@Override
public boolean authenticate(String username, String password, ServerSession session) {
return "ftpuser".equals(username) && "topsecret".equals(password);
}
});
List<NamedFactory<UserAuth>> userAuthFactories = new ArrayList<NamedFactory<UserAuth>>();
userAuthFactories.add(new UserAuthPassword.Factory());
sshd.setUserAuthFactories(userAuthFactories);
sshd.setCommandFactory(new ScpCommandFactory());
List<NamedFactory<Command>> namedFactoryList = new ArrayList<NamedFactory<Command>>();
namedFactoryList.add(new SftpSubsystem.Factory());
sshd.setSubsystemFactories(namedFactoryList);
// prepare directory for test files
VirtualFileSystemFactory fileSystemFactory = new VirtualFileSystemFactory(ROOT.getAbsolutePath());
sshd.setFileSystemFactory(fileSystemFactory);
sshd.start();
}
示例8: authenticate
import org.apache.sshd.server.session.ServerSession; //導入依賴的package包/類
public boolean authenticate(String username, PublicKey publicKey, ServerSession session) {
// first we need to see if they have an entry.
Map<PublicKey, AuthorizedKey> map = userToPkToAuthKeyMap.get(username);
if (null == map) {
LOGGER.error("Failed to authenticate unknown user {} from {}.", username, session.getIoSession()
.getRemoteAddress());
return false;
}
AuthorizedKey ak = map.get(publicKey);
if (null == ak) {
LOGGER.error("Failed authentication of user {} from {} with unknown public key.", username, session
.getIoSession().getRemoteAddress());
return false;
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Successful authentication of user {} from {} with public key {}.", new Object[] {username,
session.getIoSession().getRemoteAddress(), ak.getAlias()});
}
return true;
}
示例9: testBuildSshRequestInfoObj
import org.apache.sshd.server.session.ServerSession; //導入依賴的package包/類
@Test
public void testBuildSshRequestInfoObj() throws ArtifactNotFoundException, IOException,
ArtifactMetaDataParseFailureException, ParseException {
IoSession ioSession = Mockito.mock(IoSession.class);
Mockito.when(ioSession.getRemoteAddress()).thenReturn(new InetSocketAddress("10.0.0.1", 9999));
ServerSession session = Mockito.mock(ServerSession.class);
Mockito.when(session.getUsername()).thenReturn("screwdrv");
Mockito.when(session.getIoSession()).thenReturn(ioSession);
SshRequestInfo request =
new SshRequestInfo.Builder(session).setStartTimestamp(1411455384909L)
.setMethod(SshRequestStatus.CREATED.getReasonPhrase())
.setStatus(SshRequestStatus.CREATED.getStatusCode()).setExitValue(0)
.setRepoName("maven-local-release").setPath("/com/yahoo/sshd/util/Utils.java")
.setSize(1024000L).build();
Assert.assertEquals(request.getStartTimestamp(), 1411455384909L);
Assert.assertEquals(request.getRemoteAddr(), "10.0.0.1");
Assert.assertEquals(request.getRepoName(), "maven-local-release");
Assert.assertEquals(request.getRequestPath(), "/com/yahoo/sshd/util/Utils.java");
Assert.assertEquals(request.getStatus(), 201);
Assert.assertEquals(request.getExitValue(), 0);
Assert.assertEquals(request.getMethod(), "PUT");
Assert.assertEquals(request.getUserName(), "screwdrv");
}
示例10: testObjectEqual
import org.apache.sshd.server.session.ServerSession; //導入依賴的package包/類
@Test
public void testObjectEqual() throws ArtifactNotFoundException, IOException, ArtifactMetaDataParseFailureException,
ParseException {
IoSession ioSession = Mockito.mock(IoSession.class);
Mockito.when(ioSession.getRemoteAddress()).thenReturn(new InetSocketAddress("10.0.0.1", 9999));
ServerSession session = Mockito.mock(ServerSession.class);
Mockito.when(session.getUsername()).thenReturn("screwdrv");
Mockito.when(session.getIoSession()).thenReturn(ioSession);
SshRequestInfo request1 =
new SshRequestInfo.Builder(session).setStartTimestamp(1411455384909L)
.setMethod(SshRequestStatus.CREATED.getReasonPhrase())
.setStatus(SshRequestStatus.CREATED.getStatusCode()).setExitValue(0)
.setRepoName("maven-local-release").setPath("/com/yahoo/sshd/util/Utils.java")
.setSize(1024000L).build();
SshRequestInfo request2 =
new SshRequestInfo.Builder(session).setStartTimestamp(1411455384909L)
.setMethod(SshRequestStatus.OK.getReasonPhrase())
.setStatus(SshRequestStatus.OK.getStatusCode()).setExitValue(0)
.setRepoName("maven-local-release").setPath("/com/yahoo/sshd/util/Utils.java")
.setSize(1024000L).build();
Assert.assertFalse(request1.equals(request2));
}
示例11: authenticate
import org.apache.sshd.server.session.ServerSession; //導入依賴的package包/類
@Override
public boolean authenticate(String username, String password, ServerSession serverSession) {
if (SshdPlugin.instance.getConfig().getString("credentials." + username).equals(password)) {
failCounts.put(username, 0);
return true;
}
SshdPlugin.instance.getLogger().info("Failed login for " + username + " using password authentication.");
try {
Thread.sleep(3000);
if (failCounts.containsKey(username)) {
failCounts.put(username, failCounts.get(username) + 1);
} else {
failCounts.put(username, 1);
}
if (failCounts.get(username) >= 3) {
failCounts.put(username, 0);
serverSession.close(true);
}
} catch (InterruptedException e) {
// do nothing
}
return false;
}
示例12: authenticate
import org.apache.sshd.server.session.ServerSession; //導入依賴的package包/類
@Override
public boolean authenticate(String username, PublicKey key,
ServerSession session) {
try {
PublicKey pk = getUserPublicKeyService().lookupUser(username);
if (pk != null) {
logger.debug("Got PublicKey for '{}'", username);
boolean b = KeyUtils.isSame(pk, key);
logger.debug("Keys match: {}", b);
return b;
}
logger.debug("Failed to find PublicKey for username '{}'", username);
} catch (SshProxyJException e) {
logger.info("Failed to check public key for user '" + username
+ "'", e);
}
return false;
}
示例13: startSshdServer
import org.apache.sshd.server.session.ServerSession; //導入依賴的package包/類
private static void startSshdServer() throws IOException {
sshd = SshServer.setUpDefaultServer();
// ask OS to assign a port
sshd.setPort(0);
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());
List<NamedFactory<UserAuth>> userAuthFactories =
new ArrayList<NamedFactory<UserAuth>>();
userAuthFactories.add(new UserAuthPassword.Factory());
sshd.setUserAuthFactories(userAuthFactories);
sshd.setPasswordAuthenticator(new PasswordAuthenticator() {
@Override
public boolean authenticate(String username, String password,
ServerSession session) {
if (username.equals("user") && password.equals("password")) {
return true;
}
return false;
}
});
sshd.setSubsystemFactories(
Arrays.<NamedFactory<Command>>asList(new SftpSubsystem.Factory()));
sshd.start();
port = sshd.getPort();
}
示例14: newThread
import org.apache.sshd.server.session.ServerSession; //導入依賴的package包/類
@Override
public Thread newThread(Runnable runnable, ServerSession session) {
if(ApplicationListener.log.isInfoEnabled()) {
ApplicationListener.log.info("{0}: Spawning thread for server session {1}", GroovyShellLibrary.LIBRARY_ID, session);
}
NotesThread result = new NotesThread(runnable);
//result.setContextClassLoader(classLoader);
ClassLoader cl = new DelegatingClassLoader(ApplicationListener.class.getClassLoader());
result.setContextClassLoader(cl);
return result;
}
示例15: authenticate
import org.apache.sshd.server.session.ServerSession; //導入依賴的package包/類
@Override
public boolean authenticate(String username, String password, ServerSession session) throws
PasswordChangeRequiredException {
try {
Authentication auth = authProvider.authenticate(
new UsernamePasswordAuthenticationToken(username, password));
session.getIoSession().setAttribute(Constants.USER, username);
session.getIoSession().setAttribute(Constants.USER_ROLES, auth.getAuthorities().stream()
.map(ga -> ga.getAuthority()).collect(Collectors.toSet()));
return true;
} catch (AuthenticationException ex) {
log.warn(ex.getMessage());
return false;
}
}