401(未经授权)使用查询字符串访问JIRA API时
我正在学习创建JWT令牌以访问JIRA的RESTAPI的教程。在不传递查询字符串(如401(未经授权)使用查询字符串访问JIRA API时,jira,jira-plugin,canonical-link,jwt,jira-rest-api,Jira,Jira Plugin,Canonical Link,Jwt,Jira Rest Api,我正在学习创建JWT令牌以访问JIRA的RESTAPI的教程。在不传递查询字符串(如/rest/api/2/project和/rest/api/2/issue/issue-KEY的情况下,我访问端点不会有任何问题,但在尝试传递查询字符串时,我会得到401个未经授权的,比如/rest/api/2/user/assignable/search?project=project-KEY 我猜我遗漏了什么,特别是规范URL的生成 以下是生成get请求和JWT令牌的代码: @Override public
/rest/api/2/project
和/rest/api/2/issue/issue-KEY
的情况下,我访问端点不会有任何问题,但在尝试传递查询字符串时,我会得到401个未经授权的
,比如/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;
}
@覆盖
public CloseableHttpResponse get(字符串url)抛出HttpException,
IOException,NoSuchAlgorithmException,ParseException,
约瑟夫例外{
CloseableHttpClient客户端=HttpClientBuilder.create()
.setUserAgent(“Kevin 6.9”).build();
String token=createToken(url,JIRAClient.Method.GET);
HttpGet方法=新的HttpGet(jwt.getBaseUrl()+url);
方法.setHeader(“授权”、“JWT”+令牌);
返回client.execute(方法);
}
/**
*创建JWT令牌
*
*@返回
*@抛出不支持的DencodingException
*@NoSuchAlgorithmException
*/
私有字符串createToken(字符串apiPath,JIRAClient.Method)
抛出不支持的编码异常,NoSuchAlgorithmException{
long issuedAt=System.currentTimeMillis()/1000L;
长期到期日期=发行日期+1000L;
字符串httpMethod=method.toString();
System.out.println(httpMethod);
字符串contextPath=“/jira”;
JwtJsonBuilder jwtBuilder=新JsonSmartJwtJsonBuilder()
.issuedAt(issuedAt).到期时间(expiresAt)
.issuer(jwt.getKey());
HashMap参数=新的HashMap();
CanonicalHttpUriRequest canonical=新的CanonicalHttpUriRequest(
httpMethod、apiPath、contextPath、参数);
System.out.println(“Canonical:+Canonical.getRelativePath());
附录HttpRequestClaims(jwtBuilder,canonical);
JwtWriterFactory JwtWriterFactory=新的NimbusJwtWriterFactory();
字符串jwtbuild=jwtBuilder.build();
字符串jwtToken=jwtWriterFactory.macSigningWriter(
SigningAlgorithm.HS256,jwt.getSharedSecret()).jsonToJwt(
JWT(已建成);
返回jwtToken;
}
请注意,我正在将一个空的HashMap
传递给CanonicalHttpUriRequest
。。。这是否正确?显然需要映射来生成适当的规范URI
请注意,我正在将一个空的HashMap
传递给
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);
}