我收到錯誤:Exception: org.springframework.web.client.HttpClientErrorException$Unauthorized: 401當嘗試通過 HttpHeader 連接到 Jira 時,憑據是在 configserver 檔案中配置的,應該是這樣的:
@Component
public class JiraHttpHeadersHelper {
@Value("${first.jira.auth.user}")
private String firstJiraAuthUser;
@Value("${first.jira.auth.psw}")
private String firstJiraAuthPsw;
public HttpHeaders jiraHeadersWithAuthentication() {
String plainCreds = firstJiraAuthUser ":" firstJiraAuthPsw;
System.out.println("Credenciales JiraServices: " plainCreds);
byte[] base64CredsBytes = Base64.getEncoder().encode(plainCreds.getBytes());
String base64Creds = new String(base64CredsBytes);
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic" base64Creds);
headers.setContentType(MediaType.APPLICATION_JSON);
System.out.println("Authorization JiraServices: " headers);
return headers;
}
}
我命令呼叫上述檔案的方法以及我在該行中得到錯誤的ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.GET,this.requestEnt, String.class);方法是:
public ResponseEntity<String> getPriorityJira() {
//Request entity created
this.requestEnt = new HttpEntity(this.jiraHttpHeadersHelper.jiraHeadersWithAuthentication());
String jql = "priority";
String url = jiraBaseURL jql;
try {
ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.GET,this.requestEnt, String.class);
System.out.println("HttpStatus" HttpStatus.OK);
if (result.getStatusCode() == HttpStatus.OK) {
return result;
} else {
logger.error("Jira Generic User maybe blocked, status from API: " result.getStatusCode() ". Body: " result.getBody());
return new ResponseEntity<>(result.getBody(), result.getStatusCode());
}
} catch(HttpClientErrorException e) {
logger.error("Error getting priorityJira. Exception: " e);
return new ResponseEntity<>(e.getStatusCode());
}
}
事實上,當我運行除錯并檢查憑據時,它會毫無問題地啟動它們。我已經搜索,嘗試了此頁面上的大多數鏈接,但找不到解決方案。在這種情況下,任何幫助將不勝感激,在此先感謝。
uj5u.com熱心網友回復:
當您定義您的授權標頭時,您將您的密鑰與“基本”連接起來,而不添加空格。
headers.add("Authorization", "Basic" base64Creds);
代替 :
headers.add("Authorization", "Basic " base64Creds);
也許就是這樣。
編輯:答案是添加StandardCharsets.UTF-8到 String 建構式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/485763.html
標籤:爪哇 春天 弹簧靴 休息模板 httphandler
上一篇:從專案的.xml檔案[不在POM檔案中]中的Internet上的.properties檔案中讀取屬性以更新模塊的版本
