|
|
@@ -11,42 +11,52 @@ import org.apache.commons.lang3.StringUtils;
|
|
|
import java.io.FileOutputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
import java.net.URL;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
- * cookie管理
|
|
|
+ * Cookie管理类,用于处理HTTP请求中的Cookie存储和加载
|
|
|
+ *
|
|
|
* @author zsq
|
|
|
- * @version 1.0
|
|
|
+ * @version 1.1
|
|
|
* @date 2021/3/31 16:17
|
|
|
*/
|
|
|
public class MyCookieStore implements CookieJar {
|
|
|
|
|
|
- /**打印日志用**/
|
|
|
+ /**
|
|
|
+ * 打印日志用,开发时设为true,发布时设为false
|
|
|
+ */
|
|
|
private static volatile boolean debug = false;
|
|
|
|
|
|
- /**登录后拿到的token**/
|
|
|
+ /**
|
|
|
+ * 登录后拿到的token
|
|
|
+ */
|
|
|
private static String TOKEN = "";
|
|
|
|
|
|
- /**管理cookie**/
|
|
|
+ /**
|
|
|
+ * 管理cookie,使用ConcurrentHashMap保证线程安全
|
|
|
+ */
|
|
|
private static ConcurrentHashMap<String, ConcurrentHashMap<String, Cookie>> cookieStore = new ConcurrentHashMap<>();
|
|
|
|
|
|
- /**token资源文件位置**/
|
|
|
+ /**
|
|
|
+ * token资源文件位置
|
|
|
+ */
|
|
|
public static final String TOKEN_FILE_PATH = "/store/token.txt";
|
|
|
|
|
|
- /**cookie资源文件位置**/
|
|
|
+ /**
|
|
|
+ * cookie资源文件位置
|
|
|
+ */
|
|
|
public static final String COOKIE_FILE_PATH = "/store/cookie.txt";
|
|
|
|
|
|
-
|
|
|
@Override
|
|
|
public void saveFromResponse(HttpUrl httpUrl, List<Cookie> list) {
|
|
|
log("-----保存cookie");
|
|
|
|
|
|
- //保存cookie
|
|
|
+ // 保存cookie
|
|
|
log("httpUrl host: " + httpUrl.host());
|
|
|
ConcurrentHashMap<String, Cookie> cookieMap = cookieStore.get(httpUrl.host());
|
|
|
if (cookieMap == null) {
|
|
|
@@ -57,8 +67,9 @@ public class MyCookieStore implements CookieJar {
|
|
|
cookieMap.put(cookie.name(), cookie);
|
|
|
}
|
|
|
|
|
|
- if (list != null && list.size() > 0) {
|
|
|
- //存到本地,需要自己写持久化的实现,可用redis,或本地
|
|
|
+ // 持久化cookie到文件(示例中未实现具体逻辑,可根据需要实现)
|
|
|
+ if (list != null && !list.isEmpty()) {
|
|
|
+ persistCookiesToFile();
|
|
|
}
|
|
|
|
|
|
log("-----保存到cookie:" + cookieStore.toString());
|
|
|
@@ -71,7 +82,7 @@ public class MyCookieStore implements CookieJar {
|
|
|
List<Cookie> cookies = new ArrayList<>();
|
|
|
Map<String, Cookie> cookieMap = cookieStore.get(httpUrl.host());
|
|
|
if (cookieMap != null) {
|
|
|
- cookieMap.forEach((name, cookie)->{
|
|
|
+ cookieMap.forEach((name, cookie) -> {
|
|
|
log("--cookie:" + name + "=" + cookie.value());
|
|
|
cookies.add(cookie);
|
|
|
});
|
|
|
@@ -82,6 +93,7 @@ public class MyCookieStore implements CookieJar {
|
|
|
|
|
|
public static void setToken(String token) {
|
|
|
TOKEN = token;
|
|
|
+ persistTokenToFile(); // 新增:更新token时持久化到文件
|
|
|
}
|
|
|
|
|
|
public static String getToken() {
|
|
|
@@ -89,24 +101,36 @@ public class MyCookieStore implements CookieJar {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 保存cookie
|
|
|
- * @param resPath 资源文件目录
|
|
|
+ * 保存cookie到文件(示例中未实现具体序列化逻辑,可根据需要实现)
|
|
|
*/
|
|
|
- public static void saveCookie (String resPath, ConcurrentHashMap<String, ConcurrentHashMap<String, Cookie>> cookieMap) {
|
|
|
+ private static void persistCookiesToFile() {
|
|
|
+ // 这里可以添加将cookieStore序列化并保存到文件的逻辑
|
|
|
+ // 例如使用JSON序列化库将cookieStore转换为字符串并写入文件
|
|
|
+ }
|
|
|
|
|
|
+ /**
|
|
|
+ * 保存token到文件
|
|
|
+ */
|
|
|
+ private static void persistTokenToFile() {
|
|
|
+ saveToFile(TOKEN_FILE_PATH, TOKEN);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 保存到文件
|
|
|
+ *
|
|
|
* @param resPath 资源文件目录
|
|
|
+ * @param str 要保存的字符串内容
|
|
|
*/
|
|
|
- public static void saveToFile (String resPath, String str) {
|
|
|
+ public static void saveToFile(String resPath, String str) {
|
|
|
URL resource = MyCookieStore.class.getResource(resPath);
|
|
|
- try {
|
|
|
- IOUtils.write(str.getBytes(), new FileOutputStream(resource.getFile()));
|
|
|
- } catch (IOException e) {
|
|
|
- System.out.println("---保存数据到本地失败");
|
|
|
- e.printStackTrace();
|
|
|
+ if (resource != null) {
|
|
|
+ try (OutputStream outputStream = new FileOutputStream(resource.getFile())) {
|
|
|
+ IOUtils.write(str.getBytes(), outputStream);
|
|
|
+ } catch (IOException e) {
|
|
|
+ System.err.println("---保存数据到本地失败: " + e.getMessage());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ System.err.println("---资源路径无效: " + resPath);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -114,18 +138,18 @@ public class MyCookieStore implements CookieJar {
|
|
|
* 加载cookie到内存
|
|
|
*/
|
|
|
public static void loadCookie() {
|
|
|
- InputStream resourceAsStream = MyCookieStore.class.getResourceAsStream(COOKIE_FILE_PATH);
|
|
|
- String cookieTxt = null;
|
|
|
- try {
|
|
|
- cookieTxt = IOUtils.toString(resourceAsStream);
|
|
|
- if (StringUtils.isNotBlank(cookieTxt)) {
|
|
|
- //转成对象
|
|
|
- ConcurrentHashMap<String, ConcurrentHashMap<String, Cookie>> nativeCookie = JsonUtils.jsonToObj(cookieTxt,
|
|
|
- new TypeReference<ConcurrentHashMap<String, ConcurrentHashMap<String, Cookie>>>() {});
|
|
|
- cookieStore = nativeCookie;
|
|
|
+ try (InputStream resourceAsStream = MyCookieStore.class.getResourceAsStream(COOKIE_FILE_PATH)) {
|
|
|
+ if (resourceAsStream != null) {
|
|
|
+ String cookieTxt = IOUtils.toString(resourceAsStream);
|
|
|
+ if (StringUtils.isNotBlank(cookieTxt)) {
|
|
|
+ // 转成对象
|
|
|
+ ConcurrentHashMap<String, ConcurrentHashMap<String, Cookie>> nativeCookie = JsonUtils.jsonToObj(cookieTxt,
|
|
|
+ new TypeReference<ConcurrentHashMap<String, ConcurrentHashMap<String, Cookie>>>() {});
|
|
|
+ cookieStore = nativeCookie;
|
|
|
+ }
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
+ System.err.println("---加载cookie文件失败: " + e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -133,21 +157,19 @@ public class MyCookieStore implements CookieJar {
|
|
|
* 加载token到内存
|
|
|
*/
|
|
|
public static void loadToken() {
|
|
|
- InputStream resourceAsStream = MyCookieStore.class.getResourceAsStream(TOKEN_FILE_PATH);
|
|
|
- String s = null;
|
|
|
- try {
|
|
|
- s = IOUtils.toString(resourceAsStream);
|
|
|
- TOKEN = s;
|
|
|
+ try (InputStream resourceAsStream = MyCookieStore.class.getResourceAsStream(TOKEN_FILE_PATH)) {
|
|
|
+ if (resourceAsStream != null) {
|
|
|
+ String s = IOUtils.toString(resourceAsStream);
|
|
|
+ TOKEN = s;
|
|
|
+ }
|
|
|
} catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
+ System.err.println("---加载token文件失败: " + e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static void log(String log) {
|
|
|
- if (!debug) {
|
|
|
- return;
|
|
|
+ if (debug) {
|
|
|
+ System.out.println(log);
|
|
|
}
|
|
|
- System.out.println(log);
|
|
|
}
|
|
|
-
|
|
|
-}
|
|
|
+}
|