Я пытаюсь выполнить api. Этот API запускает сборку Jenkins. Я кодирую свое имя пользователя и пароль, а затем устанавливаю их в заголовки. Но это дает мне ошибку ниже
org.springframework.web.client.HttpClientErrorException: 401 Malformed HTTP basic Authorization header.
API отлично работает с этим завитком:
curl -kX POST --header 'Content-Type: application/json' --header 'Accept: application/json' 'https://XYZ' --user "username:password"
Джав программа:
TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(csf).build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
String encodeBytes = Base64.getEncoder().encodeToString(("username" + "password").getBytes());
ManagementController.restTemplate = new RestTemplate(requestFactory);
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Basic " + encodeBytes);
headers.setContentType(org.springframework.http.MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<String>(headers);
ResponseEntity<String> response = restTemplate.exchange(deployURL,HttpMethod.POST, entity, String.class);
String message=response.getBody();
return modelAndView;
Заранее спасибо!




Client side[edit]
When the user agent wants to send authentication credentials to the server, it may use the Authorization field.
The Authorization field is constructed as follows:[6]
- The username and password are combined with a single colon (:). This means that the username itself cannot contain a colon.
- The resulting string is encoded into an octet sequence. The character set to use for this encoding is by default unspecified, as long as it is compatible with US-ASCII, but the server may suggest use of UTF-8 by sending the charset parameter.[7]
- The resulting string is encoded using a variant of Base64.
- The authorization method and a space (e.g. "Basic ") is then prepended to the encoded string.
For example, if the browser uses Aladdin as the username and OpenSesame as the password, then the field's value is the base64-encoding of Aladdin:OpenSesame, or QWxhZGRpbjpPcGVuU2VzYW1l. Then the Authorization header will appear as:
Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l-- https://en.wikipedia.org/wiki/Basic_access_authentication#Client_side
Я думаю, вы забыли поставить двоеточие между именем пользователя и паролем здесь:
String encodeBytes = Base64.getEncoder().encodeToString(("username" + "password").getBytes());
Используйте этот метод и передайте имя пользователя и пароль в заголовках
private HttpHeaders createHeaders(final String username, final String password) {
return new HttpHeaders() {
{
if (!StringUtils.isBlank(username)) {
String auth = username + ":" + password;
byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")));
String authHeader = "Basic " + new String(encodedAuth);
set("Authorization", authHeader);
}
}
};
}