我正在尝试使用 JDK 11 HttpClient通过需要通过登录名和密码进行身份验证的公司代理发出请求。根据JDK的介绍,我正在通过以下方式构建客户端实例:
HttpClient httpClient = HttpClient.newBuilder()
.version(HTTP_1_1)
.proxy(ProxySelector.of(new InetSocketAddress("proxy.mycompany.com", 3128)))
.authenticator(authenticator)
.build();
Run Code Online (Sandbox Code Playgroud)
,哪里authenticator是:
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("login", "password".toCharArray());
}
};
Run Code Online (Sandbox Code Playgroud)
然后我执行请求本身:
HttpRequest outRequest = HttpRequest.newBuilder()
.version(HTTP_1_1)
.GET()
.uri(URI.create("https://httpwg.org/asset/http.svg")) // no matter which URI to request
.build();
HttpResponse<String> inResponse = httpClient.send(outRequest, BodyHandlers.ofString());
Run Code Online (Sandbox Code Playgroud)
但是,我收到的不是来自目标服务器 ( https://httpwg.org )的有效响应,而是HTTP 407(需要代理身份验证),即HttpClient不使用提供的authenticator.
我已经尝试了这里和这里提到的各种解决方案,但没有一个有帮助。
使它工作的正确方法是什么?
我有一个带有以下 docker 文件的 Spring Boot 应用程序。
FROM docker.com/base/jdk1.8:latest
MAINTAINER Application Engineering [ https://docker.com/ ]
RUN mkdir -p /opt/docker/svc
COPY application/weather-service.war /opt/docker/svc/
CMD java -jar /opt/docker/svc/weather-service.war --spring.config.location=file:/conf/application.properties -Dlogging.config=/conf/logback.xml
Run Code Online (Sandbox Code Playgroud)
我可以将 kubernetes configMap 或 secrets 用于 application.properties,并使用如下所示的卷挂载选项。
"spec": {
"volumes": [
{
"name": "svc-prop",
"configMap": {
"name": "svc-app-config",
"items": [
{
"key": "application.properties",
"path": "application.properties"
}
]
}
}
],
"containers": [
"volumeMounts": [
{
"name": "svc-prop",
"mountPath": "/conf"
}
]
Run Code Online (Sandbox Code Playgroud)
我如何才能为 logback.xml 实现相同的目标。在这种情况下,我是否需要将机密用作文件?
我不想将 logback.xml 文件与图像捆绑在一起,因为我们可能会在运行时更改日志级别。
还有其他更好的方法可以在 Kubernetes 中为 Spring Boot 应用程序保留 …