OAuth authentication in jira by using Java [closed

2019-01-29 05:58发布

I want to send REST request to create issues in jira from my java application. So i haven't got problems with sending request, and issue creates perfectly while i create issue through browser(cause if i'm not mistake JIRA use cookies to autorisation) Can anybody give me advice how to implement OAuth jira authorisation in Java? I never did this before i think it's not difficult but i don't know what i need to start. I find this OAuth example and this REST request documentation for JIRA. Can you advice me any doc's about OAuth or any examples with java autorisation.

Thanks in advanced.

2条回答
▲ chillily
2楼-- · 2019-01-29 06:38

Here's my answer to the same question in the Atlassian forum:

It is very hacky but it is compatible with JiraRestClientFactory. First follow JIRA REST API Tutorial to get your access token. Read their source code to understand exactly what they do, as my code is based on their code. Their code uses net.oauth library. Since this library is not under central maven repository, you need to add its repository to your maven file:

    <repository>
        <id>oauth</id>
        <url>http://oauth.googlecode.com/svn/code/maven/</url>
    </repository>
    ...

    <dependency>
        <groupId>net.oauth.core</groupId>
        <artifactId>oauth</artifactId>
        <version>20100527</version>
    </dependency>

    <dependency>
        <groupId>net.oauth.core</groupId>
        <artifactId>oauth-consumer</artifactId>
        <version>20100527</version>
    </dependency>

    <dependency>
        <groupId>net.oauth.core</groupId>
        <artifactId>oauth-httpclient4</artifactId>
        <version>20090913</version>
    </dependency>

Now, we need to combine what the tutorial did to make authenticated requests with the new AuthenticationHandler.

@Test
public void test() throws Exception {
        final JiraRestClientFactory factory = new AsynchronousJiraRestClientFactory();
        final URI jiraServerUri = new URI("https://your.jira.instance");
        JiraRestClient restClient = null;
        //          restClient = factory.createWithBasicHttpAuthentication(jiraServerUri, "user", "pass"); // old way of doing it
        restClient = factory.create(jiraServerUri, new AuthenticationHandler() {
            @Override
            public void configure(Request request) {
                try {
                    OAuthAccessor accessor = getAccessor();
                    accessor.accessToken = access_token;
                    OAuthMessage request2 = accessor.newRequestMessage(null, request.getUri().toString(), Collections.<Map.Entry<?, ?>>emptySet(), request.getEntityStream());
                    Object accepted = accessor.consumer.getProperty(OAuthConsumer.ACCEPT_ENCODING);
                    if (accepted != null) {
                        request2.getHeaders().add(new OAuth.Parameter(HttpMessage.ACCEPT_ENCODING, accepted.toString()));
                    }
                    Object ps = accessor.consumer.getProperty(OAuthClient.PARAMETER_STYLE);
                    ParameterStyle style = (ps == null) ? ParameterStyle.BODY
                            : Enum.valueOf(ParameterStyle.class, ps.toString());
                    HttpMessage httpRequest = HttpMessage.newRequest(request2, style);
                    for ( Entry<String, String> ap : httpRequest.headers)
                        request.setHeader(ap.getKey(), ap.getValue());
                    request.setUri( httpRequest.url.toURI() );
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        final int buildNumber = restClient.getMetadataClient().getServerInfo().claim().getBuildNumber();

        assertTrue(buildNumber > 0);
}

private final OAuthAccessor getAccessor()
{
    if (accessor == null)
    {
        OAuthServiceProvider serviceProvider = new OAuthServiceProvider(getRequestTokenUrl(), getAuthorizeUrl(), getAccessTokenUrl());
        OAuthConsumer consumer = new OAuthConsumer(callback, consumerKey, null, serviceProvider);
        consumer.setProperty(RSA_SHA1.PRIVATE_KEY, privateKey);
        consumer.setProperty(OAuth.OAUTH_SIGNATURE_METHOD, OAuth.RSA_SHA1);
        accessor = new OAuthAccessor(consumer);
    }
    return accessor;
}
private String getAccessTokenUrl()
{
    return baseUrl + SERVLET_BASE_URL + "/oauth/access-token";
}
private String getRequestTokenUrl()
{
    return  baseUrl + SERVLET_BASE_URL + "/oauth/request-token";
}
public String getAuthorizeUrlForToken(String token)
{
    return getAuthorizeUrl() + "?oauth_token=" + token;
}
private String getAuthorizeUrl() {return baseUrl + SERVLET_BASE_URL + "/oauth/authorize";}
查看更多
趁早两清
3楼-- · 2019-01-29 06:39

I'm assuming you've been here already, but you can't get much better than the official website for documentation on how it works.

(Ported over from my comment as requested)

查看更多
登录 后发表回答