5

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

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

There are probably several ways to do it. One might be using 'su' -command and seeing if the login is succesful or not. It might also be possible to parse the password file, but that sounds quite unsecure.
Actually with su command i can verify the username but not the password of a given user can u suggest a command to verify both parameters ?
0

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;
    } 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.