fix cookie bug

This commit is contained in:
Looly 2019-10-10 16:24:48 +08:00
parent ccff387646
commit 96cf2503cc
5 changed files with 61 additions and 54 deletions

View File

@ -9,3 +9,4 @@
* 【all】 升级JDK最低支持到8
### Bug修复
* 【http】 修复Cookie中host失效导致的问题

View File

@ -214,7 +214,7 @@ public class URLUtil {
}
/**
* 获取URL中域名部分
* 获取URL中域名部分只保留URL中的协议ProtocolHost其它为null
*
* @param url URL
* @return 域名的URI
@ -224,7 +224,12 @@ public class URLUtil {
if(null == url){
return null;
}
return toURI(url.getHost());
try {
return new URI(url.getProtocol(), url.getHost(), null, null);
} catch (URISyntaxException e) {
throw new UtilException(e);
}
}
/**

View File

@ -62,10 +62,10 @@ public class URLUtilTest {
@Test
public void getHostTest() throws MalformedURLException {
String url = "//www.hutool.cn//aaa/\\bbb?a=1&b=2";
String url = "https://www.hutool.cn//aaa/\\bbb?a=1&b=2";
String normalize = URLUtil.normalize(url);
URI host = URLUtil.getHost(new URL(normalize));
Assert.assertEquals("www.hutool.cn", host.toString());
Assert.assertEquals("https://www.hutool.cn", host.toString());
}
@Test

View File

@ -14,7 +14,7 @@ import java.util.List;
import java.util.Map;
/**
* 全局Cooki管理器只针对Hutool请求有效
* 全局Cookie管理器只针对Hutool请求有效
*
* @author Looly
* @since 4.5.15
@ -53,7 +53,7 @@ public class GlobalCookieManager {
* @since 4.6.9
*/
public static List<HttpCookie> getCookies(HttpConnection conn){
return cookieManager.getCookieStore().get(getDomain(conn));
return cookieManager.getCookieStore().get(getURI(conn));
}
/**
@ -69,11 +69,12 @@ public class GlobalCookieManager {
Map<String, List<String>> cookieHeader;
try {
cookieHeader = cookieManager.get(getDomain(conn), new HashMap<String, List<String>>(0));
cookieHeader = cookieManager.get(getURI(conn), new HashMap<String, List<String>>(0));
} catch (IOException e) {
throw new IORuntimeException(e);
}
// 不覆盖模式回填Cookie头这样用户定义的Cookie将优先
conn.header(cookieHeader, false);
}
@ -90,18 +91,18 @@ public class GlobalCookieManager {
}
try {
cookieManager.put(getDomain(conn), conn.headers());
cookieManager.put(getURI(conn), conn.headers());
} catch (IOException e) {
throw new IORuntimeException(e);
}
}
/**
* 获取连接的URL中域名信息例如http://www.hutool.cn/aaa/bb.html得到www.hutool.cn
* 获取连接的URL中URI信息
* @param conn HttpConnection
* @return URI
*/
private static URI getDomain(HttpConnection conn){
return URLUtil.getHost(conn.getUrl());
private static URI getURI(HttpConnection conn){
return URLUtil.toURI(conn.getUrl());
}
}