我有自己使用GitServlet实现的自定义git servlet 我有自己的身份验证。
要克隆或推送,拉我想对我的身份验证插件进行身份验证。 为此,我需要从用户提供的URL中读取用户/密码。
像,
git clone http://sohanb:welcome@localhost:8080/git.ctr-0.0.1-SNAPSHOT/portal/.git
我如何获得用户名,即sohanb和密码,即welcome。
更新了问题:
我的git init代码,
public void init(ServletConfig config) throws ServletException {
username = config.getInitParameter("username");
password=config.getInitParameter("password");
idpUrl=config.getInitParameter("idpUrl");
System.out.println("IDP URL is =======>> " + idpUrl);
setRepositoryResolver(new RepositoryResolver<HttpServletRequest>() {
public Repository open(HttpServletRequest req, String name) throws RepositoryNotFoundException,
ServiceNotEnabledException {
try {
System.out.println("name =============>> " +name);
System.out.println("parmrs if any ==========>> " + req.getParameterMap());
System.out.println("URL ===============>>" + req.getRequestURL().toString());
URL url = new URL(req.getRequestURL().toString());
System.out.println("Trying to get uswer info if any ====>> " +url.getUserInfo());
File gitParentFolder = new File("D:\\sigma-admin\\git.ctr\\git");
File gitRepoFolder = new File(gitParentFolder, name);
System.out.println("gitRepoFolder.exists() =======>>" + gitRepoFolder.exists());
if (gitRepoFolder.exists()) {
if (!gitRepoFolder.getAbsolutePath().endsWith(".git")) {
gitRepoFolder = new File(gitRepoFolder, ".git");
}
Repository db = Git.open(gitRepoFolder).getRepository();
db.incrementOpen();
return db;
} else {
throw new RepositoryNotFoundException("Unknown repository was requested by client " + name);
}
} catch (IOException e) {
throw new RepositoryNotFoundException(name, e);
}
}
});
}
我的git war在jettry服务器下运行
http://localhost:8080/git.ctr-0.0.1-SNAPSHOT/
现在我做git clone http://sohanb:welcome@localhost:8080/git.ctr-0.0.1-SNAPSHOT/portal/.git
我可以在日志/控制台中看到这个网址,
http://localhost:8080/git.ctr-0.0.1-SNAPSHOT/portal/.git/
我不知道为什么使用:从req url传递丢失。或者它无效我想要实现的目标。
其他建议是最受欢迎的。 请建议。
答案 0 :(得分:0)
正则表达式将是:
https?:\/\/(?<user>[^:]+):(?<password>[^@]+)
# look for http or https followed by ://
# capture everything up (but not including) a colon and capture it in a group called user
# match a colon
# capture everything up (but not including) an @ and capture it in a group called password
在此处查看demo。然而,它在某种程度上取决于你真正想要的东西(使用PHP,Python,bash等中的信息)。
答案 1 :(得分:-1)
由于它是http:
,您可以使用java.net.URL
:
URL url = new URL("http://sohanb:welcome@localhost:8080/git.ctr-0.0.1-SNAPSHOT/portal/.git");
String userInfo = url.getUserInfo(); // userInfo = "sohanb:welcome"
检查null,然后你可以轻松获得你想要的东西:
String[] userInfoArray = userInfo.split(":");
String username = userInfoArray[0]; // sohanb
String password = userInfoArray[1]; // welcome