0

我正在按照此处找到的教程创建 JWT 令牌以访问 JIRA 的 REST API。我在不传递查询字符串的情况下访问端点没有任何问题/rest/api/2/project/rest/api/2/issue/ISSUE-KEY但是401 Unauthorized当我尝试传递查询字符串时,比如说/rest/api/2/user/assignable/search?project=PROJECT-KEY

我猜我错过了一些东西,特别是规范 URL 的生成,

以下是生成 get 请求和 JWT 令牌的代码:

@Override
public CloseableHttpResponse get(String url) throws HttpException,
        IOException, NoSuchAlgorithmException, ParseException,
        JOSEException {
    CloseableHttpClient client = HttpClientBuilder.create()
            .setUserAgent("Kevin 6.9").build();
    String token = createToken(url, JIRAClient.Method.GET);
    HttpGet method = new HttpGet(jwt.getBaseUrl() + url);
    method.setHeader("Authorization", "JWT " + token);
    return client.execute(method);
}

/**
 * Create JWT token
 * 
 * @return
 * @throws UnsupportedEncodingException
 * @throws NoSuchAlgorithmException
 */
private String createToken(String apiPath, JIRAClient.Method method)
        throws UnsupportedEncodingException, NoSuchAlgorithmException {
    long issuedAt = System.currentTimeMillis() / 1000L;
    long expiresAt = issuedAt + 1000L;
    String httpMethod = method.toString();
    System.out.println(httpMethod);

    String contextPath = "/jira";

    JwtJsonBuilder jwtBuilder = new JsonSmartJwtJsonBuilder()
            .issuedAt(issuedAt).expirationTime(expiresAt)
            .issuer(jwt.getKey());

    HashMap<String, String[]> parameters = new HashMap<String, String[]>();
    CanonicalHttpUriRequest canonical = new CanonicalHttpUriRequest(
            httpMethod, apiPath, contextPath, parameters);
    System.out.println("Canonical : " + canonical.getRelativePath());
    JwtClaimsBuilder.appendHttpRequestClaims(jwtBuilder, canonical);

    JwtWriterFactory jwtWriterFactory = new NimbusJwtWriterFactory();
    String jwtbuilt = jwtBuilder.build();
    String jwtToken = jwtWriterFactory.macSigningWriter(
            SigningAlgorithm.HS256, jwt.getSharedSecret()).jsonToJwt(
            jwtbuilt);

    return jwtToken;
}

请注意,我将一个空传递HashMap<String, String[]>CanonicalHttpUriRequest... 这是正确的吗?

4

1 回答 1

0

显然Map<String, String[]>需要生成适当的规范 URI。

请注意,我将一个空传递HashMap<String, String[]>CanonicalHttpUriRequest... 这是正确的吗?

我修改了我的方法签名,以便可以将其作为参数传递。注意:createQueryString是我的类中的一个方法,它从参数映射手动创建查询字符串。

@Override
public CloseableHttpResponse get(String url,
        @SuppressWarnings("rawtypes") Map parameters) throws Exception {
    CloseableHttpClient client = HttpClientBuilder.create()
            .setUserAgent("Kevin 5.0").build();
    String token = createToken(url, JIRAClient.Method.GET, parameters);
    HttpGet method = new HttpGet(jwt.getBaseUrl() + url
            + createQueryString(parameters));
    method.setHeader("Authorization", "JWT " + token);
    return client.execute(method);
}

它有效。

@Test
public void testJQL() throws Exception {
    HashMap param = new HashMap();
    param.put("jql", new String[] {"project=COR"});
    param.put("startAt", new String[] {"0"});
    HttpResponse response = client.get("/rest/api/2/search", param);
    Assert.assertTrue(response.getStatusLine().getStatusCode() == 200);
}
于 2014-06-16T03:19:44.457 回答