From 33a014825fd97031d17655936c210f9c7bf450fb Mon Sep 17 00:00:00 2001 From: zhouwentao <1577701412@qq.com> Date: Fri, 8 Sep 2023 18:17:11 +0800 Subject: [PATCH] updates --- .../com/example/zxweb/utils/CacheManager.java | 21 +++++++++++++++++++ .../com/example/zxweb/utils/RestUtil.java | 18 ++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 src/main/java/com/example/zxweb/utils/CacheManager.java diff --git a/src/main/java/com/example/zxweb/utils/CacheManager.java b/src/main/java/com/example/zxweb/utils/CacheManager.java new file mode 100644 index 0000000..d5943d8 --- /dev/null +++ b/src/main/java/com/example/zxweb/utils/CacheManager.java @@ -0,0 +1,21 @@ +package com.example.zxweb.utils; + +import java.util.HashMap; +import java.util.Map; + +/** + * @Description + * @Author ZhouWenTao + * @Date 2023/9/8 18:11 + */ +public class CacheManager { + private static Map cache = new HashMap<>(); + + public static void put(String key, Object value) { + cache.put(key, value); + } + + public static Object get(String key) { + return cache.get(key); + } +} diff --git a/src/main/java/com/example/zxweb/utils/RestUtil.java b/src/main/java/com/example/zxweb/utils/RestUtil.java index f24025d..6c9f1d2 100644 --- a/src/main/java/com/example/zxweb/utils/RestUtil.java +++ b/src/main/java/com/example/zxweb/utils/RestUtil.java @@ -10,6 +10,7 @@ import org.springframework.web.client.RestTemplate; import java.nio.charset.StandardCharsets; import java.util.Iterator; +import java.util.List; import java.util.Map; /** @@ -195,6 +196,11 @@ public class RestUtil { headers.add(s,header.getString(s)); } } + String cookie =(String) CacheManager.get("Cookie"); + if (StringUtils.isNotBlank(cookie)) { + headers.add("Cookie",cookie); + } + return request(url, method, headers, variables, params, JSONObject.class); } @@ -254,6 +260,18 @@ public class RestUtil { RT = new RestTemplate(requestFactory); // 解决乱码问题 RT.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8)); + ResponseEntity exchange = RT.exchange(url, method, request, responseType); + HttpHeaders responseBodyHeaders = exchange.getHeaders(); + List stringList = responseBodyHeaders.get("Set-Cookie"); + StringBuilder sb=new StringBuilder(); + for (String s : stringList) { + if (s.contains("JSESSIONID") &&!s.contains("deleteMe")) { + sb.append(s); + } + } + if (StringUtils.isNotBlank(sb.toString())) { + CacheManager.put("Cookie",sb.toString()); + } return RT.exchange(url, method, request, responseType); }