我正在用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
而不是我想要打印,无效凭据。
请让我知道我哪里出错了,我该如何解决这个问题。
由于
答案 0 :(得分:1)
你去:
try (Git result = Git.cloneRepository().setURI(REMOTE_URL) {
...
} catch (TransportException te) {
System.out.println("Invalid credentials");
}
例如,。
您应该不告诉用户该帐户是否存在。如:如果你告诉攻击者,他可以断定他已经拥有一个有效的用户名。