quarkus-microprofile rest-client 1.3为何忽略代理用户名密码系统环境属性?
Quarkus 1.3 microprofile rest-client 代理认证失效问题解答
根本原因
Quarkus 1.3.x版本集成的microprofile rest-client底层默认使用RESTEasy Client实现,该版本的RESTEasy Client对JDK标准代理参数的支持存在限制:仅会主动读取https.proxyHost、https.proxyPort(对应HTTP代理同理)配置代理地址,不会默认加载https.proxyUser、https.proxyPassword参数做代理身份认证,这是版本固有特性,不属于配置错误。
而Google HTTP Client这类第三方客户端本身做了额外的兼容逻辑,会主动读取系统属性里的代理用户名、密码参数自动完成认证,所以会出现同项目不同客户端表现不一致的情况。
解决方案
确实需要自行配置代理身份认证,有两种可落地的方案:
方案1:全局配置JDK Authenticator
在应用启动阶段注册全局默认的Authenticator,适配所有使用JDK代理逻辑的客户端:
@Startup @ApplicationScoped public class ProxyAuthenticatorConfig { public void init(@ConfigProperty(name = "https.proxyUser") String proxyUser, @ConfigProperty(name = "https.proxyPassword") String proxyPassword) { Authenticator.setDefault(new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { if (getRequestorType() == RequestorType.PROXY) { return new PasswordAuthentication(proxyUser, proxyPassword.toCharArray()); } return super.getPasswordAuthentication(); } }); } }
注意:该配置会全局生效,如果应用内有其他不需要走该代理认证的请求逻辑,需要做额外的路由判断
方案2:单独为Rest Client配置代理认证
如果不想修改全局认证逻辑,可以针对单个Rest Client手动指定代理认证信息:
@RegisterRestClient @RegisterProvider(ProxyAuthenticatorFilter.class) public interface YourRemoteService { // 你的接口定义 } // 自定义过滤器实现代理认证 public class ProxyAuthenticatorFilter implements ClientRequestFilter { @ConfigProperty(name = "https.proxyUser") String proxyUser; @ConfigProperty(name = "https.proxyPassword") String proxyPassword; @Override public void filter(ClientRequestContext requestContext) throws IOException { String credential = Base64.getEncoder().encodeToString((proxyUser + ":" + proxyPassword).getBytes(StandardCharsets.UTF_8)); requestContext.getHeaders().add("Proxy-Authorization", "Basic " + credential); } }
升级参考
如果可以升级Quarkus版本,Quarkus 2.0+版本已经优化了microprofile rest-client的代理逻辑,支持直接通过配置项quarkus.rest-client.proxy-user、quarkus.rest-client.proxy-password统一配置代理认证信息,无需自行实现认证逻辑。
内容的提问来源于stack exchange,提问作者always_learning
相关产品推荐
相关产品推荐

