Java 中的 SonarQube Web API 身份验证

Fél*_*iel 0 java api authorization token sonarqube

我正在尝试使用 SonarQube 7.9.1 CE Web API 从 Java Spring Boot 应用程序中的服务器检索一些信息,我从一个非常简单的信息开始:​​项目列表。

事实证明,我需要通过令牌或用户/密码组合提供我的身份信息。但官方文档和 StackOverflow 答案仅显示 CURL 请求作为示例:

curl -u admin:admin http://localhost:9000/api/components/search_projects
curl -u 9548utj958tju5498jt934: http://localhost:9000/api/components/search_projects
Run Code Online (Sandbox Code Playgroud)

我一直在尝试使用RestTemplate发送请求,但总是收到 401 UNAUTHORIZED 错误。

我应该如何发送用户令牌?在请求标头中?作为请求参数的一部分?没有人说。

我尝试使用Bearer 9548utj958tju5498jt934Basic 9548utj958tju5498jt934通过Authorization标头来完成此操作,在请求中插入参数,但没有任何效果。

谢谢。

小智 5

解决方案是在标记后面放置“:”字符。如果您查看文档,您将看到:

# 注意curl中需要token后面的冒号来设置空密码curl -u THIS_IS_MY_TOKEN: https://sonarqube.com/api/user_tokens/search

https://docs.sonarqube.org/latest/extend/web-api/

因此,为了使其在 java 应用程序中工作,您必须执行以下操作: String tokenBase = userToken + ":"; String basicAuth = "Basic " + new String(Base64.getEncoder().encode(tokenBase.getBytes())); urlConnection.setRequestProperty("Authorization", basicAuth);

这对我有用!