我有一个 spring boot 服务调用另一个 spring boot 服务。
两个服务都部署到 docker 容器中。
当第一个服务调用第二个服务时,第二个服务得到 401 响应。当我使用 PostMan 执行相同的请求时,我得到了有效的响应。
鉴于我通过邮递员和服务本身使用凭证。
知道为什么会发生这种情况吗?有没有跟docker部署有关?
用于调用第二个服务的代码
try {
urlStr = licenseRequest.getString(LicenseRequest.URL);
logger.info("URL: "+ urlStr);
URL url = new URL(urlStr);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json");
// auth
String authStrEncoded = Base64Utils.encodeToString((username + ":" + password).getBytes());
con.setRequestProperty("Authorization", "Basic " + authStrEncoded);
if (!licenseRequest.isNull(LicenseRequest.BODY))
{
String body = licenseRequest.getString(LicenseRequest.BODY);
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream (
con.getOutputStream());
wr.write(body.getBytes());
wr.close();
}
//Get Response
InputStream is = con.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
StringBuilder response = new StringBuilder(); // or StringBuffer if Java version 5+
String line;
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
return response.toString();
}