From 7bd83035d21f876d507225bdba7fcb925575feba Mon Sep 17 00:00:00 2001 From: wanglin Date: Thu, 26 Dec 2024 10:34:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=AD=A3=E5=BC=8F=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E7=9A=84token=E8=A7=A3=E6=9E=90=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/lh/until/Utils.java | 33 ++++++++++++++++++------------- src/main/resources/application.properties | 2 ++ 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/lh/until/Utils.java b/src/main/java/com/lh/until/Utils.java index eafd283..900a8d0 100644 --- a/src/main/java/com/lh/until/Utils.java +++ b/src/main/java/com/lh/until/Utils.java @@ -1,9 +1,8 @@ package com.lh.until; import cn.hutool.core.util.StrUtil; +import cn.hutool.extra.spring.SpringUtil; import cn.hutool.json.JSONUtil; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; import com.lh.bean.RespBean; import com.lh.bean.dto.TokenDTO; import com.lh.exception.MyException; @@ -15,23 +14,31 @@ import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; -import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Component; -import javax.annotation.Resource; +import javax.annotation.PostConstruct; import java.io.IOException; import java.nio.charset.StandardCharsets; -import java.util.Optional; +import java.util.Arrays; @Slf4j @Component public class Utils { - @Resource - private StringRedisTemplate stringRedisTemplate; + // 编码Token + static String url; + + @PostConstruct + public void initAnalysisTokenUrl() { + boolean isProd = Arrays.asList(SpringUtil.getActiveProfiles()).contains("prod"); + if (isProd) { + url = SpringUtil.getProperty("homilychart.token.analysis.url.prod"); + } else { + url = SpringUtil.getProperty("homilychart.token.analysis.url.test"); + } + } + // 获取token中的信息 public TokenDTO analysisToken(String token) throws IOException { - // 编码Token - String url = "http://39.101.133.168:8828/hljw/api/v2/member/info"; // 创建HttpClient实例 try (CloseableHttpClient httpClient = HttpClients.createDefault()) { // 创建POST请求 @@ -44,15 +51,13 @@ public class Utils { // 发送请求并获取响应 try (CloseableHttpResponse response = httpClient.execute(postRequest)) { int responseCode = response.getStatusLine().getStatusCode(); // 获取状态码 - // 检查响应状态 - if (responseCode != 200) { - throw new MyException("Failed : HTTP error code : " + responseCode); - } // 读取响应体 String responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); + log.info("[Utils] exec analysisToken url:{}, requestBody:{}, responseCode:{}, responseBody:{}", url, token, responseCode, responseBody); + // 获取不到用户状态 - if (StrUtil.isEmpty(responseBody)) { + if (responseCode != 200 || StrUtil.isEmpty(responseBody)) { throw new MyException("用户状态获取失败"); } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 3a61886..a2f2a91 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -37,3 +37,5 @@ spring.redis.jedis.pool.min-idle=1 # 每日最大投票次数 vote.limit.daily=3 +homilychart.token.analysis.url.test = http://39.101.133.168:8828/hljw/api/v2/member/info +homilychart.token.analysis.url.prod = https://api.homilychart.com/hljw/api/v2/member/info \ No newline at end of file