Abdul Khaliq
Abdul Khaliq

Reputation: 2453

Authenticate linux based user in java

I have a username and password for a particular user in Linux i need to verify that if the user is valid or not using java?

Abdul Khaliq

Upvotes: 5

Views: 3281

Answers (3)

ashishgupta_mca
ashishgupta_mca

Reputation: 578

It's too late to answer the question but may be helpful for others. JSch library http://www.jcraft.com/jsch/ can help to achieve this. Below method will return string if authentication is successful otherwise it will throw an exception.

public String authenticate(String host, String user, String password) throws JSchException {
        JSch jsch = new JSch();
        Session session = jsch.getSession(user, host, 22);
        session.setPassword(password);
        Properties obj_Properties = new Properties();
        obj_Properties.put("StrictHostKeyChecking", "no");
        session.setConfig(obj_Properties);
        session.connect(5000);
        String version = session.getServerVersion();
        session.disconnect();
        return version;
    } 

Upvotes: 0

Michael Borgwardt
Michael Borgwardt

Reputation: 346290

The Java way to do this would be JAAS, but you'll still need a LoginModule that works with Linux. Here's a beta implementation that claims to work.

Upvotes: 5

simoraman
simoraman

Reputation: 939

You could use Java Runtime-object to run the command line commands that suit your needs. Runtime API

Upvotes: 1

Related Questions