java - 如何使用Jsoup登录JIRA?

标签 java jsoup jira

我一直在尝试使用 Jsoup API 登录 JIRA,但由于某种原因无法正常工作。

我尝试识别用户名和密码标签,但仍然不行。

有人可以帮我弄清楚我做错了什么吗?

public class test
{
public static void main(String[] args) throws IOException{
    final String USER_AGENT = "\"Mozilla/5.0 (Windows NT\" +\n" +  
     "          \" 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36\"";  
    String loginFormUrl = "https://jira.aciworldwide.com/login.jsp";  
    String loginActionUrl = "https://jira.aciworldwide.com/login.jsp";  
    String username = "jon@jon";  
    String password = "XXXXXX";  

     HashMap<String, String> cookies = new HashMap<>();  
     HashMap<String, String> formData = new HashMap<>();  

     Connection.Response loginForm = Jsoup.connect(loginFormUrl).method(Connection.Method.GET).userAgent(USER_AGENT).execute();  
     Document loginDoc = loginForm.parse(); // this is the document that contains response html

     cookies.putAll(loginForm.cookies()); // save the cookies, this will be passed on to next request  

     formData.put("login", "Log In"); 
     formData.put("os_username", username);  
     formData.put("os_password", password); 

     Connection.Response homePage = Jsoup.connect(loginActionUrl)  
         .cookies(cookies)  
         .data(formData)  
         .method(Connection.Method.POST)  
         .userAgent(USER_AGENT)  
         .execute();  

     System.out.println(homePage.parse().html());  


   }
}

如果不是 JSoup,是否还有其他可用的 API?

任何帮助将不胜感激!

最佳答案

你的方向是正确的,但有时“伪造”某些网络应用程序的登录过程可能非常棘手或很难成功,因为你缺少设置一个特定的 header 、cookie或一些特殊参数...

如果你看一下 Jira 的 documentation你会发现很多例子。您最好执行 REST POST 来获取有效的 cookie,而不是将用户名和密码作为表单发送。

正如文档中所述:

  • (1) The client creates a new session for the user, via the JIRA REST API .
  • (2) JIRA returns a session object, which has information about the session including the session cookie. The client stores this session object.
  • (3) The client can now set the cookie in the header for all subsequent requests to the JIRA REST API.

我创建了一个简单的类来测试它,并且我确保它可以与 Jira 一起使用(不确定版本是什么,但可能是最后一个)。 方法getFullName只是在服务器的响应中搜索fullName(成功登录后fullName将出现在响应中):

import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.util.Map;
import java.util.Optional;

public class JiraLogin {

private final static String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36";
private final static String JIRA_REST_LOGIN = "https://youdomain.com/rest/auth/1/session";

private final static String HOME_URL = "https://youdomain.com/";
private final static String USERNAME = "your_username";
private final static String PASSWORD = "your_password";

public static void main(String[] args) throws IOException {
    JiraLogin app = new JiraLogin();
    app.doLogin();
}

public void doLogin() throws IOException {
    // (1)
    Response postResult = doLoginPost();
    System.out.println("POST credentials result: " + postResult.body());
    // (2)
    Map<String, String> cookies = postResult.cookies();

    Document loggedDocument = Jsoup.connect(HOME_URL)
            .cookies(cookies)    // (3)
            .method(Method.GET)
            .userAgent(USER_AGENT)
            .validateTLSCertificates(false)
            .get();

    System.out.println("FullName: " + getFullName(loggedDocument));
}

private Response doLoginPost() throws IOException {
    return Jsoup.connect(JIRA_REST_LOGIN)
            .validateTLSCertificates(false)
            .method(Method.POST)
            // if use regular USER_AGENT gets a 403 error
            // http://stackoverflow.com/questions/10120849/jsoup-connect-throws-403-error-while-apache-httpclient-is-able-to-fetch-the-cont
            .userAgent("Mozilla")
            .ignoreContentType(true)
            .requestBody("{ \"username\": \"" + USERNAME +"\", \"password\": \"" + PASSWORD +"\" }")
            .header("Content-Type", "application/json")
            .execute();
}

private String getFullName(Document document) {
    Optional<Element> fullNameOpt = document.getElementsByTag("meta")
            .stream()
            .filter(e -> e.hasAttr("name") && "ajs-remote-user-fullname".equals(e.attr("name"))).findFirst();

    return fullNameOpt.isPresent() ? fullNameOpt.get().attr("content") : "Not found";
}

}

关于java - 如何使用Jsoup登录JIRA?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43388575/

相关文章:

java - 读取 XML 文件 java

java - Jenkins : Cannot run program "docker": error=2, 没有那个文件或目录

java - 如何从网页上抓取一些数据

java - 如何使用 JSoup 通过表单提交文本

java - 获取 css 内容,然后为链接 validator 解析外部 css 文件的图像引用

java - 如何在 cassandra 2.2 中获得前 5 条记录

java - 斯坦福 CoreNLP 仅在 Windows 上失败

jira - 在 Confluence 中为 Confluence 的匿名用户显示 JIRA 内容

testing - 测试用例管理工具2017

Jira JQL - 显示所有子任务