|
|
@@ -1,6 +1,7 @@
|
|
|
package com.cyksj.service.authorization.impl;
|
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.core.util.URLUtil;
|
|
|
import com.cyksj.common.EnvCommonService;
|
|
|
import com.cyksj.common.util.Codec;
|
|
|
import com.cyksj.config.WeChatConfig;
|
|
|
@@ -16,7 +17,6 @@ import java.net.MalformedURLException;
|
|
|
import java.net.URL;
|
|
|
import java.net.URLEncoder;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
-import java.util.Optional;
|
|
|
|
|
|
/**
|
|
|
* @author chan
|
|
|
@@ -36,13 +36,39 @@ public class AuthorizationServiceImpl implements AuthorizationService {
|
|
|
private final EnvCommonService envCommonService;
|
|
|
|
|
|
|
|
|
+ // 提取获取Host的逻辑,避免重复创建URL对象
|
|
|
+ private String getHostFromUrl(String url) throws MalformedURLException {
|
|
|
+ return URLUtil.getHost(new URL(url)).getHost();
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public String createUrl(String url, String state, String authType) throws MalformedURLException {
|
|
|
- String preUrl = Optional.ofNullable(gerUrlDomain(url)).orElse(weChatConfig.getDomain());
|
|
|
- log.info("回调url:{}", preUrl);
|
|
|
- return weChatService.createUrl(state, preUrl + WeChatAuthRedirectUrl.get(authType).getUrl()
|
|
|
- , weChatConfig.getAppid(), WeChatAuthRedirectUrl.get(authType).getScope());
|
|
|
+ // 获取重定向URL的Host
|
|
|
+ String redirectHost = getHostFromUrl(url);
|
|
|
+
|
|
|
+ // 从配置中获取预设的回调URL,并获取其Host
|
|
|
+ String preUrl = weChatConfig.getDomain();
|
|
|
+ String authHost = getHostFromUrl(preUrl);
|
|
|
+
|
|
|
+ // 记录回调URL日志
|
|
|
+ log.info("回调url: {}", preUrl);
|
|
|
+
|
|
|
+ // 构造最终的重定向URL
|
|
|
+ String weChatRedirectUrl = weChatConfig.getDomain() + WeChatAuthRedirectUrl.get(authType).getUrl();
|
|
|
+
|
|
|
+ // 如果重定向URL和授权域名不同,则追加 "/other"
|
|
|
+ if (!redirectHost.equals(authHost)) {
|
|
|
+ weChatRedirectUrl += "/other";
|
|
|
+ }
|
|
|
+
|
|
|
+ return weChatService.createUrl(
|
|
|
+ state,
|
|
|
+ weChatRedirectUrl,
|
|
|
+ weChatConfig.getAppid(),
|
|
|
+ WeChatAuthRedirectUrl.get(authType).getScope()
|
|
|
+ );
|
|
|
}
|
|
|
+
|
|
|
@Override
|
|
|
public JsSdkTicket getJsTicket(String appId, String url) throws Exception {
|
|
|
|