You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
package com.lh.until;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.lh.bean.dto.TokenDTO; import lombok.extern.slf4j.Slf4j; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; 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 java.io.IOException; import java.nio.charset.StandardCharsets;
@Slf4j @Component public class Utils { @Resource private StringRedisTemplate stringRedisTemplate; // 获取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请求
HttpPost postRequest = new HttpPost(url); postRequest.setHeader("Content-Type", "application/x-www-form-urlencoded"); System.out.println(token); // 设置请求体参数
StringEntity entity = new StringEntity(token, ContentType.APPLICATION_FORM_URLENCODED); postRequest.setEntity(entity); // 发送请求并获取响应
try (CloseableHttpResponse response = httpClient.execute(postRequest)) { int responseCode = response.getStatusLine().getStatusCode(); // 获取状态码
// 检查响应状态
if (responseCode == 200) { // 读取响应体
String responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); // 使用Jackson解析JSON响应体
ObjectMapper objectMapper = new ObjectMapper(); JsonNode jsonResponse = objectMapper.readTree(responseBody); JsonNode dataNode = jsonResponse.get("data"); TokenDTO tokenDTO = new TokenDTO(); tokenDTO.setJwcode(dataNode.get("jwcode").asInt()); tokenDTO.setUsername(dataNode.get("username").asText()); return tokenDTO; } else { throw new RuntimeException("Failed : HTTP error code : " + responseCode); } } } }
// 获取token中的jwcode
public Integer getJwcode(String token) throws IOException { TokenDTO dto = analysisToken(token); return dto.getJwcode(); } public String getUsername(String token) throws IOException { TokenDTO dto = analysisToken(token); return dto.getUsername(); }
}
|