如何使用 jGit 授权用户
how to authorize an user using jGit
我正在 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
我想打印的不是这个,无效的凭据。
请告诉我哪里出错了,我该如何解决。
谢谢
你去:
try (Git result = Git.cloneRepository().setURI(REMOTE_URL) {
...
} catch (TransportException te) {
System.out.println("Invalid credentials");
}
例如。
您应该不告诉用户该帐户是否存在。如:如果您告诉攻击者,他可以得出结论,他已经获得了有效的用户名。
我正在 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
我想打印的不是这个,无效的凭据。
请告诉我哪里出错了,我该如何解决。
谢谢
你去:
try (Git result = Git.cloneRepository().setURI(REMOTE_URL) {
...
} catch (TransportException te) {
System.out.println("Invalid credentials");
}
例如。
您应该不告诉用户该帐户是否存在。如:如果您告诉攻击者,他可以得出结论,他已经获得了有效的用户名。