我一直在尝试使用 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?
任何帮助将不胜感激!
您的方向是正确的,但有时“伪造”某些 web 应用程序的登录过程可能非常棘手或难以成功,因为您缺少设置一个特定的标题、cookie 或一些特殊的参数...
如果您查看 Jira 的文档,您会发现许多示例。与其将用户名和密码作为表单发送,不如执行 REST POST 以获取有效的 cookie。
正如文档中所说:
- (1) 客户端通过 JIRA REST API 为用户创建一个新会话。
- (2) JIRA 返回一个会话对象,该对象包含有关会话的信息,包括会话 cookie。客户端存储这个会话对象。
- (3) 客户端现在可以为 JIRA REST API 的所有后续请求在标头中设置 cookie。
我创建了一个简单的类来测试它,并且我已经确保与 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";
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句