【发布时间】:2018-10-30 17:02:14
【问题描述】:
我正在用 Java 创建一个应用程序并使用 jGit。作为其中的一部分,我需要对用户进行身份验证。我想输出用户是否存在。目前我得到一个例外为user is not authorized。下面是我的代码。
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());
}
}
}
当我运行上面的代码时,如果我提供正确的凭据,我会得到输出
Having repository:XXXXX
如果我提供错误的凭据,我会收到错误
Exception in thread "main" org.eclipse.jgit.api.errors.TransportException: https://myRepo.git: not authorized
我想打印的不是这个,而是无效的凭据。
请让我知道我哪里出错了,我该如何解决这个问题。
谢谢
【问题讨论】:
-
@GhostCat 我的错,一开始就应该这样做:)