j0h4nn3s
j0h4nn3s

Reputation: 2076

JSch authenticate user on SSH server

I'm writing an application using JSch. This application should authenticate the user once and then store the username and password in that session. Therefor I need to connect to the server once just for authentication. I will later only read files from the server, so only SFTP permissions are needed.

Since now I'll authenticate the user with reading the content of a predefined file. But this is not good for performance and the file has to exist on the server. Is there any other, better solution?

EDIT: I'll try to clarify my problem here, so if you didn't understand what I am trying to do, read on.

What I want to have: I have an application, where you have to log in at first. When a user logs in, the username and password are stored in the users session and will be used for all requests. After log in, the user can read some files from the server. These files are accessed over SFTP with the previous saved credentials.

The problem: I don't know how to validate the users credentials, as soon as he logs in to the application, without accessing files on the server.

My workaround: I try to solve the problem, with reading a predefined small file from the server during log in and accept the credentials, when the file could be read.

My question: I would like to know, if there is any better solution, like a function to just authenticate the user on the server, so I can be sure, the entered credentials are correct. Or maybe, what would be even better, I can open a connection to the server, when the user logs in, and use this connection for further requests, without sending the credentials.

Upvotes: 1

Views: 581

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202534

Just create a new Session and call .connect().

Session session = jsch.getSession("username", "host");
session.setPassword("password");
session.connect();
session.disconnect();

You must have this code in place already for the transfers.


Anyway, you are right that a better solution is definitely to keep the session opened.

Just connect, keep the reference to the Session instance and reuse it for all future transfers.

Upvotes: 2

Related Questions