Regex 从http git URL获取用户名和密码

Regex 从http git URL获取用户名和密码,regex,git,servlets,Regex,Git,Servlets,我使用GitServlet实现了自己的定制GitServlet。 我有自己的身份验证 要克隆或推送,请拉我要根据我的身份验证插件进行身份验证。 为此,我需要从用户提供的url中读取用户/密码 像 我怎样才能获得usernmae,即sohanb和password,即welcome 更新问题: 我的git init代码 public void init(ServletConfig config) throws ServletException { username = config.

我使用GitServlet实现了自己的定制GitServlet。
我有自己的身份验证

要克隆或推送,请拉我要根据我的身份验证插件进行身份验证。 为此,我需要从用户提供的url中读取用户/密码

我怎样才能获得usernmae,即sohanb和password,即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克隆时http://sohanb:welcome@localhost:8080/git.ctr-0.0.1-SNAPSHOT/portal/.git 我可以在日志/控制台中看到的url

http://localhost:8080/git.ctr-0.0.1-SNAPSHOT/portal/.git/
我不知道为什么使用:从req url中缺少传递。或者,我试图实现的目标是无效的

其他这样做的建议最受欢迎。 请建议。

正则表达式应为:

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
https?:\/\/(?[^:]+):(?[^@]+)
#查找http或https,后跟://
#捕获冒号上的所有内容(但不包括冒号),并将其捕获到名为user的组中
#匹配冒号
#捕获@上的所有内容(但不包括@),并将其捕获到名为password的组中

在这里看到一张照片。但是,这在某种程度上取决于您真正想要的内容(使用PHP、Python、bash等语言的信息)。

由于它是
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

在servelet中,如何获取URL。我在码头下面跑。当我执行req.getRequestURL().toString()时,我只得到一个。我在URL中看不到用户名:密码。什么样的servelt方法在这里应该有用?我得到的最新url是。我希望传入的url用户/传入的url丢失。我不确定出了什么问题。这场战争是在servelet的指挥下进行的。
URL url = new URL("http://sohanb:welcome@localhost:8080/git.ctr-0.0.1-SNAPSHOT/portal/.git");
String userInfo = url.getUserInfo(); // userInfo = "sohanb:welcome"
String[] userInfoArray = userInfo.split(":");
String username = userInfoArray[0]; // sohanb
String password = userInfoArray[1]; // welcome