Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 28, 2025

OkHttp代理认证使用了错误的认证头Authorization,应该使用Proxy-Authorization。同时应使用proxyAuthenticator()而非authenticator()方法。

修改内容

  • WxCpServiceOkHttpImpl.java - 修复代理认证配置
  • WxMaServiceOkHttpImpl.java - 修复代理认证配置
  • WxChannelServiceOkHttpImpl.java - 修复代理认证配置
  • WxQidianServiceOkHttpImpl.java - 修复代理认证配置
  • WxCpTpServiceOkHttpImpl.java - 修复代理认证配置

注:WxMpServiceOkHttpImpl.java 已使用正确配置,无需修改。

修改示例

// Before
clientBuilder.authenticator(new Authenticator() {
    public Request authenticate(Route route, Response response) {
        String credential = Credentials.basic(username, password);
        return response.request().newBuilder()
            .header("Authorization", credential)
            .build();
    }
});

// After
clientBuilder.proxyAuthenticator(new Authenticator() {
    public Request authenticate(Route route, Response response) {
        String credential = Credentials.basic(username, password);
        return response.request().newBuilder()
            .header("Proxy-Authorization", credential)
            .build();
    }
});

Fixes #3331

Original prompt

This section details on the original issue you should resolve

<issue_title>使用http代理时且需认证时,http客户端认证头设置错误</issue_title>
<issue_description>### 简要描述
使用http代理时且需认证时,http客户端认证头设置错误

模块版本情况

  • WxJava 模块名: wx-java-mp-spring-boot-starter-4.6.5.B WxMpServiceOkHttpImpl
  • WxJava 版本号:4.6.5.B

详细描述

clientBuilder.authenticator(new Authenticator() {
                public Request authenticate(Route route, Response response) throws IOException {
                    String credential = Credentials.basic(WxMpServiceOkHttpImpl.this.httpProxy.getProxyUsername(), WxMpServiceOkHttpImpl.this.httpProxy.getProxyPassword());
                    return response.request().newBuilder().header("Authorization", credential).build();
                }
            });

修正意见:

Tinyproxy 默认情况下 ​不支持直接修改认证请求头(如 Proxy-Authorization)的名称或格式,其认证机制严格遵循 HTTP 协议标准。以下是详细分析和替代方案:

​1. Tinyproxy 的认证头固定性
​标准协议约束​
Tinyproxy 的认证流程基于 HTTP 协议规范,使用固定的请求头和响应头:

代理要求认证时返回:Proxy-Authenticate: Basic realm="..."
客户端提交凭证时携带:Proxy-Authorization: Basic <credentials>

日志

me.chanjar.weixin.common.error.WxRuntimeException: java.io.IOException: Failed to authenticate with proxy
	at me.chanjar.weixin.mp.api.impl.BaseWxMpServiceImpl.getAccessToken(BaseWxMpServiceImpl.java:287)
	at me.chanjar.weixin.mp.api.impl.BaseWxMpServiceImpl.executeInternal(BaseWxMpServiceImpl.java:471)
	at me.chanjar.weixin.mp.api.impl.BaseWxMpServiceImpl.execute(BaseWxMpServiceImpl.java:435)
	at me.chanjar.weixin.mp.api.impl.BaseWxMpServiceImpl.post(BaseWxMpServiceImpl.java:383)
	at me.chanjar.weixin.mp.api.impl.BaseWxMpServiceImpl.post(BaseWxMpServiceImpl.java:398)
	at me.chanjar.weixin.mp.api.impl.WxMpFreePublishServiceImpl.getPublicationRecords(WxMpFreePublishServiceImpl.java:64)
	at me.chanjar.weixin.mp.api.impl.WxMpFreePublishServiceImpl.getPublicationRecords(WxMpFreePublishServiceImpl.java:70)
	at cn.iocoder.yudao.module.mp.controller.admin.news.MpFreePublishController.getFreePublishPage(MpFreePublishController.java:55)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:355)

</issue_description>

Comments on the Issue (you are @copilot in this section)

@binarywang 欢迎直接提供PR修复代码

Custom agent used: 全部用中文
需要用中文,包括PR标题和分析总结过程


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…oxy-Authorization

Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix incorrect authentication header for Http client with proxy 修复HTTP代理认证头设置错误 Nov 28, 2025
Copilot AI requested a review from binarywang November 28, 2025 14:06
Copilot finished work on behalf of binarywang November 28, 2025 14:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

使用http代理时且需认证时,http客户端认证头设置错误

2 participants