I’m getting the error: Exception: org.springframework.web.client.HttpClientErrorException$Unauthorized: 401
, when trying to connect to Jira through HttpHeader, and the credentials are configured in a configserver file, which would be this:
@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; } }
And the method where I command to call the file above and where I get the error on the line ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.GET,this.requestEnt, String.class);
, would be this:
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()); } }
In fact, when I run the debug and check the credentials, it brings them up without a problem. I’ve already searched, tried most of the links on this page and I can’t find the solution. Any help would be appreciated in this case, thanks in advance.
Advertisement
Answer
When you define your authorization header you concat your key with « Basic » without adding a white space.
headers.add("Authorization", "Basic" + base64Creds);
Instead of :
headers.add("Authorization", "Basic " + base64Creds);
Maybe it’s just that.
Edit :
The answer was to add StandardCharsets.UTF-8
to the String constructor.