Procházet zdrojové kódy

其他域名授权,中转

chenbiao před 1 rokem
rodič
revize
5e0cb1fe80

+ 31 - 5
netflix-service/src/main/java/com/cyksj/service/authorization/impl/AuthorizationServiceImpl.java

@@ -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 {
 

+ 49 - 0
netflix-web/src/main/java/com/cyksj/web/controller/user/AuthorizationController.java

@@ -164,6 +164,55 @@ public class AuthorizationController {
 
     }
 
+    /**
+     * 全量授权
+     *
+     * @author chan
+     * @date 2021-10-11 上午11:57
+     */
+    @GetMapping(value = "/weChat/baseRedirect/other")
+    public void redirectOther(String code, String state, HttpServletResponse response) throws Exception {
+        // 转跳参数
+        String json = Codec.DoBase64.custom().setData(state).decodeAsText();
+        AuthRedirect re = Jsons.parseObject(json, AuthRedirect.class);
+
+
+        //  查询用户期望转跳的url
+        String url = redisService.getStr(re.getMappingId().toString());
+        String requestUrl = request.getRequestURL().toString();
+        log.info("非主域名授权请求地址:{}", requestUrl);
+
+        log.info("微信非主域名授权转跳. url: {}", url);
+        String path = URLUtil.getPath(weChatConfig.getDomain());
+        response.sendRedirect(URLUtil.getHost(new URL(url)).getHost()  + path + "/weChat/baseRedirect" + "?code=" + code + "&state=" + state);
+    }
+
+    /**
+     * 静默授权
+     *
+     * @author chan
+     * @date 2021-10-11 上午11:58
+     */
+    @GetMapping("/weChat/silentRedirect/other")
+    @Transactional(rollbackFor = Throwable.class)
+    public void redirectV2Other(String code, String state, HttpServletResponse response) throws Exception {
+        // 转跳参数
+        String json = Codec.DoBase64.custom().setData(state).decodeAsText();
+        AuthRedirect re = Jsons.parseObject(json, AuthRedirect.class);
+
+
+        //  查询用户期望转跳的url
+        String url = redisService.getStr(re.getMappingId().toString());
+        String requestUrl = request.getRequestURL().toString();
+        log.info("非主域名授权请求地址:{}", requestUrl);
+
+        log.info("微信非主域名授权转跳. url: {}", url);
+        String path = URLUtil.getPath(weChatConfig.getDomain());
+
+        response.sendRedirect(URLUtil.getHost(new URL(url)).getHost()  + path + "/weChat/silentRedirect" + "?code=" + code + "&state=" + state);
+    }
+
+
     /**
      * 微信js-sdk 权限验证
      */