我正在使用 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());
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行上面的代码时,如果我提供正确的凭据,我得到的输出为
Having repository:XXXXX
Run Code Online (Sandbox Code Playgroud)
如果我提供错误的凭据,我会收到错误消息
Exception in thread "main" org.eclipse.jgit.api.errors.TransportException: https://myRepo.git: not authorized
Run Code Online (Sandbox Code Playgroud)
相反,我想打印无效的凭据。
请让我知道我哪里出了问题以及如何解决这个问题。
谢谢
你去:
try (Git result = Git.cloneRepository().setURI(REMOTE_URL) {
...
} catch (TransportException te) {
System.out.println("Invalid credentials");
}
Run Code Online (Sandbox Code Playgroud)
例如。
您不应该告诉用户该帐户是否存在。例如:如果您告诉攻击者,他可以断定他已经获得了有效的用户名。