how to authorize an user using jGit

元气小坏坏 提交于 2019-12-24 00:37:41

问题


I'm creating an application in Java and using jGit. As part of this I need to authenticate an user. I want to output if the user is existing or not. Currently I get an exception as user is not authorized. Below is my code.

import java.io.File;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;

public class AuthenticateanUser {

    public static void main(String[] args) throws Exception {
        final String REMOTE_URL = "https://myRepo.git";
        // prepare a new folder for the cloned repository
        File localPath = File.createTempFile("TestGitRepository", "");
        localPath.delete();
        // then clone
        try (Git result = Git.cloneRepository().setURI(REMOTE_URL)
                .setCredentialsProvider(new UsernamePasswordCredentialsProvider("myId", "myPwd"))
                .setDirectory(localPath).call()) {

            System.out.println("Having repository: " + result.status());
        }
    }

}

when I run my above code, If I give correct credentials, I get the output as

Having repository:XXXXX

if I give wrong credentials I get error as

Exception in thread "main" org.eclipse.jgit.api.errors.TransportException: https://myRepo.git: not authorized

Instead of this I want to print, Invalid credentials.

please let me know where am I going wrong and how can I fix this.

Thanks


回答1:


You go:

try (Git result = Git.cloneRepository().setURI(REMOTE_URL) {
  ...         
} catch (TransportException te) {
     System.out.println("Invalid credentials");
}

for example.

You should not tell the user if the account exists or not. As in: if you tell that an attacker, he can conclude that he already got a valid username.



来源:https://stackoverflow.com/questions/50442064/how-to-authorize-an-user-using-jgit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!