Browse Source

修改正式环境的token解析地址

master
王琳 5 months ago
parent
commit
7bd83035d2
  1. 33
      src/main/java/com/lh/until/Utils.java
  2. 2
      src/main/resources/application.properties

33
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("用户状态获取失败");
}

2
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
Loading…
Cancel
Save