我正在尝试使用 SonarQube 7.9.1 CE Web API 在 Java Spring Boot 应用程序中从服务器检索一些信息,我从一个非常简单的信息开始:项目列表。
事实证明,我需要通过 token 或用户/密码组合提供我的身份信息。但官方文档和 StackOverflow 答案仅显示 CURL 请求作为示例:
curl -u admin:admin http://localhost:9000/api/components/search_projects
curl -u 9548utj958tju5498jt934: http://localhost:9000/api/components/search_projects
我一直在尝试使用 RestTemplate 发送请求,但总是收到 401 UNAUTHORIZED 错误。
我应该如何发送用户 token ? 在请求 header 中?作为请求参数的一部分?没有人说。
我尝试使用Bearer 9548utj958tju5498jt934、Basic 9548utj958tju5498jt934通过Authorization header 来完成此操作,在请求中插入参数,但没有任何效果.
谢谢。
最佳答案
解决方案是在标记后面放置“:”字符。 如果您查看文档,您将看到:
# note that the colon after the token is required in curl to set an empty password 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 = "基本"+ new String(Base64.getEncoder().encode(tokenBase.getBytes()));
urlConnection.setRequestProperty("授权", basicAuth);
这对我有用!
关于java - Java 中的 SonarQube Web API 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59278377/