-
-
Notifications
You must be signed in to change notification settings - Fork 9k
修复会话存档API使用错误secret导致权限异常的问题 #3873
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
9
commits into
develop
Choose a base branch
from
copilot/fix-session-archive-secret-issue
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+622
−3
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4398b34
Initial plan
Copilot 0fa17f4
为四个WxCpService实现类添加getMsgAuditAccessToken方法
Copilot d49e21c
修复测试代码中的重复调用
Copilot 9d81192
修复Redis配置NPE问题并改进测试覆盖
Copilot c23b540
修复测试代码中的var类型声明以提高兼容性
Copilot 677ffe0
改进代码注释的准确性和完整性
Copilot 6a678d1
移除测试中的Thread.sleep以提高测试稳定性
Copilot 1eeb41d
重构测试代码以消除重复逻辑
Copilot 7a70b85
改进Redis配置类的注释文档
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |
| import okhttp3.*; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.concurrent.locks.Lock; | ||
|
|
||
| import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.GET_TOKEN; | ||
|
|
||
|
|
@@ -74,6 +75,52 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException { | |
| return this.configStorage.getAccessToken(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getMsgAuditAccessToken(boolean forceRefresh) throws WxErrorException { | ||
| if (!this.configStorage.isMsgAuditAccessTokenExpired() && !forceRefresh) { | ||
| return this.configStorage.getMsgAuditAccessToken(); | ||
| } | ||
|
|
||
| Lock lock = this.configStorage.getMsgAuditAccessTokenLock(); | ||
| lock.lock(); | ||
| try { | ||
| // 拿到锁之后,再次判断一下最新的token是否过期,避免重刷 | ||
| if (!this.configStorage.isMsgAuditAccessTokenExpired() && !forceRefresh) { | ||
| return this.configStorage.getMsgAuditAccessToken(); | ||
| } | ||
| // 使用会话存档secret获取access_token | ||
| String msgAuditSecret = this.configStorage.getMsgAuditSecret(); | ||
| if (msgAuditSecret == null || msgAuditSecret.trim().isEmpty()) { | ||
| throw new WxErrorException("会话存档secret未配置"); | ||
| } | ||
| //得到httpClient | ||
| OkHttpClient client = getRequestHttpClient(); | ||
| //请求的request | ||
| Request request = new Request.Builder() | ||
| .url(String.format(this.configStorage.getApiUrl(GET_TOKEN), this.configStorage.getCorpId(), | ||
| msgAuditSecret)) | ||
| .get() | ||
| .build(); | ||
| String resultContent = null; | ||
| try (Response response = client.newCall(request).execute()) { | ||
| resultContent = response.body().string(); | ||
| } catch (IOException e) { | ||
| log.error(e.getMessage(), e); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
|
|
||
| WxError error = WxError.fromJson(resultContent, WxType.CP); | ||
| if (error.getErrorCode() != 0) { | ||
| throw new WxErrorException(error); | ||
| } | ||
| WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); | ||
| this.configStorage.updateMsgAuditAccessToken(accessToken.getAccessToken(), | ||
| accessToken.getExpiresIn()); | ||
| } finally { | ||
| lock.unlock(); | ||
| } | ||
| return this.configStorage.getMsgAuditAccessToken(); | ||
| } | ||
|
|
||
| @Override | ||
| public void initHttp() { | ||
| log.debug("WxCpServiceOkHttpImpl initHttp"); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里通过
postWithoutToken+ 手动拼接access_token绕开了BaseWxCpServiceImpl.execute(...)的 token 失效自动刷新/重试逻辑;如果会话存档 token 被提前失效(未到本地过期时间),这些接口可能会直接失败而不会触发刷新。Other Locations
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMsgAuditServiceImpl.java:328weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMsgAuditServiceImpl.java:341🤖 Was this useful? React with 👍 or 👎