Commit defe628026b35bd46c5a275f19b89272a1e4f303

Authored by cherishsince
1 parent 2c21f713

优化请求 header 中 Referer 规则

yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/interactions/MjClient.java
1 1 package cn.iocoder.yudao.framework.ai.midjourney.interactions;
2 2  
  3 +import cn.iocoder.yudao.framework.ai.midjourney.MidjourneyConfig;
3 4 import org.springframework.http.HttpEntity;
4 5 import org.springframework.http.HttpHeaders;
5 6 import org.springframework.http.MediaType;
... ... @@ -18,16 +19,18 @@ public class MjClient {
18 19 private static RestTemplate restTemplate = new RestTemplate();
19 20 private static HttpHeaders headers = new HttpHeaders();
20 21  
  22 + private static final String HEADER_REFERER = "https://discord.com/channels/%s/%s";
  23 +
21 24 static {
22 25 headers.setContentType(MediaType.APPLICATION_JSON); // 设置内容类型为JSON
23   - headers.set("Referer", "https://discord.com/channels/1221445697157468200/1221445862962630706");
24 26 headers.set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36");
25 27 headers.set("Cookie", "__dcfduid=6ca536c0e3fa11eeb7cbe34c31b49caf; __sdcfduid=6ca536c1e3fa11eeb7cbe34c31b49caf52cce5ffd8983d2a052cf6aba75fe5fe566f2c265902e283ce30dbf98b8c9c93; _gcl_au=1.1.245923998.1710853617; _ga=GA1.1.111061823.1710853617; __cfruid=6385bb3f48345a006b25992db7dcf984e395736d-1712124666; _cfuvid=O09la5ms0ypNptiG0iD8A6BKWlTxz1LG0WR7qRStD7o-1712124666575-0.0.1.1-604800000; locale=zh-CN; cf_clearance=l_YGod1_SUtYxpDVeZXiX7DLLPl1DYrquZe8WVltvYs-1712124668-1.0.1.1-Hl2.fToel23EpF2HCu9J20rB4D7OhhCzoajPSdo.9Up.wPxhvq22DP9RHzEBKuIUlKyH.kJLxXJfAt2N.LD5WQ; OptanonConsent=isIABGlobal=false&datestamp=Wed+Apr+03+2024+14%3A11%3A15+GMT%2B0800+(%E4%B8%AD%E5%9B%BD%E6%A0%87%E5%87%86%E6%97%B6%E9%97%B4)&version=6.33.0&hosts=&landingPath=https%3A%2F%2Fdiscord.com%2F&groups=C0001%3A1%2CC0002%3A1%2CC0003%3A1; _ga_Q149DFWHT7=GS1.1.1712124668.4.1.1712124679.0.0.0");
26 28 }
27 29  
28   - public static String post(String url, String token, String body) {
29   - // 设置token
30   - headers.set("Authorization", token);
  30 + public static String post(String url, MidjourneyConfig config, String body) {
  31 + // 设置 header
  32 + headers.set("Referer", String.format(HEADER_REFERER, config.getGuildId(), config.getChannelId()));
  33 + headers.set("Authorization", config.getToken());
31 34 // 封装请求体和头部信息
32 35 HttpEntity<String> requestEntity = new HttpEntity<>(body, headers);
33 36 // 发送请求
... ...
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/interactions/MjInteractions.java
... ... @@ -40,7 +40,7 @@ public class MjInteractions {
40 40 // 设置参数
41 41 String requestBody = MjClient.setParams(requestTemplate, requestParams);
42 42 // 发送请求
43   - String res = MjClient.post(url, midjourneyConfig.getToken(), requestBody);
  43 + String res = MjClient.post(url, midjourneyConfig, requestBody);
44 44 // 这个 res 只要不返回值,就是成功!
45 45 boolean isSuccess = StrUtil.isBlank(res);
46 46 if (isSuccess) {
... ... @@ -64,7 +64,7 @@ public class MjInteractions {
64 64 // 设置参数
65 65 String requestBody = MjClient.setParams(requestTemplate, requestParams);
66 66 // 发送请求
67   - String res = MjClient.post(url, midjourneyConfig.getToken(), requestBody);
  67 + String res = MjClient.post(url, midjourneyConfig, requestBody);
68 68 // 这个 res 只要不返回值,就是成功!
69 69 boolean isSuccess = StrUtil.isBlank(res);
70 70 if (isSuccess) {
... ...