2014-06-13 84 views
0

我正在按照教程here創建一個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使用查詢字符串訪問JIRA API時的401(未授權)

我猜我錯過了一些東西,specificially規範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 ...這是正確的?

回答

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); 
}