如何使用java登录外部页面?

时间:2015-09-18 11:10:04

标签: java jsp jira

我想从java程序(特别是从jsp页面)登录JIRA。当我单击链接时,页面应该获取我存储在java类(AuthenticationDetails.java)中的身份验证详细信息。然后它应该将它添加到JIRA页面登录请求。最后,我应该能够输入JIRA而无需手动输入身份验证。 jsp页面由外部html页面触发。

这是我正在使用的代码。

<%@page import="org.apache.tomcat.util.codec.binary.Base64"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@page import="com.test.AuthenticationDetails"%>
<%@page import="org.apache.http.*"%>

<%!String username = "";
    String password = "";%>
<%
    username = AuthenticationDetails.USERNAME;
    password = AuthenticationDetails.PASSWORD;

    String headerKey="Authorization";
    String headerValue="Basic"+Base64.encodeBase64String((username+":"+password).getBytes());
    response.setHeader(headerKey,headerValue);
    response.sendRedirect("https://jira.xxx.yyy.com/login.jsp");

%>

但是这会将我重定向到登录页面。为什么不自动登录?我是一个相当陌生的java并且正在从事这个项目并且从未参与过需要Web访问的应用程序。我知道这是JIRA特有的,但是我可以为其他网站做这些,让我们说gmail等吗?在JIRA中,我注意到当我查看查询时,用户名和密码将作为os_usernameos_password传递。

当我尝试登录我在机器上创建的登录页面时,这似乎有效。

2 个答案:

答案 0 :(得分:0)

JIRA的作者提供了很少的webservices API,假设这是交换数据的官方和安全的方式。学习这项技术也是对其他项目的良好投资。

这种“登录黑客”到前端页面没有意义,因为很短的几年

REST

https://developer.atlassian.com/jiradev/jira-apis/jira-rest-apis

SOAP

https://developer.atlassian.com/jiradev/jira-apis/jira-api-status/jira-rpc-services/creating-a-jira-soap-client

和其他人

答案 1 :(得分:0)

网络抓取和自动化是我一直在寻找的。 JIRA Rest客户帮助我只从JIRA获取数据。使用像Jaunt这样的库,可以自动登录。该库有助于实现这一目标,而无需了解所有底层细节,如http标题,响应和内容。