This commit is contained in:
Leo
2026-01-15 16:13:16 +08:00
parent 61f66d90b4
commit ba1e025326

View File

@@ -52,26 +52,59 @@ public class WPS365CallbackController extends BaseController {
/** /**
* OAuth回调 - 通过授权码获取访问令牌POST请求 * OAuth回调 - 通过授权码获取访问令牌POST请求
* WPS365在验证回调URL时会发送POST请求进行challenge验证 * WPS365在验证回调URL时会发送POST请求进行challenge验证
* 支持application/json和application/x-www-form-urlencoded两种格式
*/ */
@Anonymous @Anonymous
@PostMapping @PostMapping(consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_FORM_URLENCODED_VALUE, MediaType.TEXT_PLAIN_VALUE})
public ResponseEntity<?> oauthCallbackPost(@RequestParam(value = "code", required = false) String code, public ResponseEntity<?> oauthCallbackPost(@RequestParam(value = "code", required = false) String code,
@RequestParam(value = "state", required = false) String state, @RequestParam(value = "state", required = false) String state,
@RequestParam(value = "error", required = false) String error, @RequestParam(value = "error", required = false) String error,
@RequestParam(value = "error_description", required = false) String errorDescription, @RequestParam(value = "error_description", required = false) String errorDescription,
@RequestParam(value = "challenge", required = false) String challenge, @RequestParam(value = "challenge", required = false) String challenge,
@RequestBody(required = false) String requestBody) { @RequestBody(required = false) String requestBody) {
// 如果requestBody是JSON格式尝试解析challenge log.info("收到WPS365 POST回调请求 - code: {}, challenge: {}, requestBody: {}",
if (challenge == null && requestBody != null && requestBody.trim().startsWith("{")) { code != null ? "" : "",
try { challenge != null ? challenge : "",
com.alibaba.fastjson2.JSONObject json = com.alibaba.fastjson2.JSON.parseObject(requestBody); requestBody != null && requestBody.length() > 0 ? requestBody.substring(0, Math.min(100, requestBody.length())) : "");
if (json.containsKey("challenge")) {
challenge = json.getString("challenge"); // 如果challenge在URL参数中直接使用
// 如果不在URL参数中尝试从请求体中解析可能是JSON或form-data
if (challenge == null && requestBody != null && !requestBody.trim().isEmpty()) {
String bodyTrimmed = requestBody.trim();
// 尝试解析JSON格式
if (bodyTrimmed.startsWith("{")) {
try {
com.alibaba.fastjson2.JSONObject json = com.alibaba.fastjson2.JSON.parseObject(requestBody);
if (json.containsKey("challenge")) {
challenge = json.getString("challenge");
log.info("从JSON请求体中解析到challenge: {}", challenge);
}
} catch (Exception e) {
log.debug("解析JSON请求体失败", e);
} }
} catch (Exception e) { }
log.debug("解析POST请求体失败使用URL参数", e); // 尝试解析form-urlencoded格式 (challenge=xxx)
else if (bodyTrimmed.contains("challenge=")) {
try {
String[] pairs = bodyTrimmed.split("&");
for (String pair : pairs) {
if (pair.startsWith("challenge=")) {
challenge = java.net.URLDecoder.decode(pair.substring("challenge=".length()), "UTF-8");
log.info("从form-urlencoded请求体中解析到challenge: {}", challenge);
break;
}
}
} catch (Exception e) {
log.debug("解析form-urlencoded请求体失败", e);
}
}
// 如果请求体就是challenge值本身纯文本
else if (bodyTrimmed.length() < 200) {
challenge = bodyTrimmed;
log.info("将请求体作为challenge值: {}", challenge);
} }
} }
return handleOAuthCallback(code, state, error, errorDescription, challenge, requestBody); return handleOAuthCallback(code, state, error, errorDescription, challenge, requestBody);
} }