好久沒寫博客了,一直沒什么好寫的奥洼,最近碰到JSESSIONID這個(gè)問題巷疼,網(wǎng)上的說法有點(diǎn)模糊,特別是什么時(shí)候會(huì)出現(xiàn)URL重寫這個(gè)問題灵奖,有些說客戶端禁用Cookie嚼沿,有些說第一次訪問,這里總結(jié)一下
JSESSIONID是什么
老實(shí)說一開始看到這個(gè)有點(diǎn)懵瓷患,寫Java這么久沒看過這東西骡尽。
首先,JSESSIONID是一個(gè)Cookie擅编,Servlet容器(tomcat攀细,jetty)用來記錄用戶session。
什么時(shí)候種下JSESSIONID
創(chuàng)建會(huì)話時(shí)爱态,即調(diào)用request.getSession()的時(shí)候谭贪,關(guān)于getSession就不說了。補(bǔ)充幾點(diǎn)是锦担,訪問html是不會(huì)創(chuàng)建session的故河,JSP頁面默認(rèn)是會(huì)創(chuàng)建session的,可以在JSP頁面里面關(guān)掉自動(dòng)創(chuàng)建session
URL重寫
服務(wù)端在內(nèi)存里創(chuàng)建session吆豹,需要種Cookie鱼的,除了在request header里面設(shè)置Set-Cookie以外理盆,tomcat等容器有一個(gè)URL重寫機(jī)制。這個(gè)機(jī)制是客戶端Cookie不可用時(shí)的一個(gè)兜底策略凑阶,通過在URL后面加上;jsessionid=xxx來傳遞session id猿规,這樣即使Cookie不可用時(shí),也可以保證session的可用性宙橱,但是session暴露在URL里姨俩,本身是不安全的,到這里基本網(wǎng)上說法都是一致的
但是最關(guān)鍵的問題师郑,tomcat怎么知道客戶端Cookie不可用环葵。我在idea導(dǎo)入tomcat的源碼調(diào)試跟蹤,不同版本有些出入宝冕,大致應(yīng)該還是一樣的
tomcat有一個(gè)org.apache.catalina.connector.Response是Response的落地類张遭,有兩個(gè)方法會(huì)進(jìn)行URL重寫,分別是encodeRedirectURL
和encodeURL
地梨,encodeRedirectURL
是重定向時(shí)會(huì)被調(diào)用菊卷,encodeURL
貌似是手動(dòng)調(diào)用,所以默認(rèn)情況宝剖,重定向時(shí)才會(huì)出現(xiàn)URL重寫洁闰。兩個(gè)方法代碼類似,下面只關(guān)注encodeRedirectURL
/**
* Encode the session identifier associated with this response
* into the specified redirect URL, if necessary.
*
* @param url URL to be encoded
* @return <code>true</code> if the URL was encoded
*/
@Override
public String encodeRedirectURL(String url) {
if (isEncodeable(toAbsolute(url))) {
return (toEncoded(url, request.getSessionInternal().getIdInternal()));
} else {
return (url);
}
}
方法注釋寫得很清楚了万细,如果有必要的話扑眉,把session id塞到重定向的URL里面。再看一下isEncodeable
方法赖钞,關(guān)鍵地方我加了中文注釋
/**
* Return <code>true</code> if the specified URL should be encoded with
* a session identifier. This will be true if all of the following
* conditions are met:
* <ul>
* <li>The request we are responding to asked for a valid session
* <li>The requested session ID was not received via a cookie
* <li>The specified URL points back to somewhere within the web
* application that is responding to this request
* </ul>
*
* @param location Absolute URL to be validated
* @return <code>true</code> if the URL should be encoded
*/
protected boolean isEncodeable(final String location) {
if (location == null) {
return false;
}
// Is this an intra-document reference?
if (location.startsWith("#")) {
return false;
}
// Are we in a valid session that is not using cookies?
final Request hreq = request;
final Session session = hreq.getSessionInternal(false);
if (session == null) {
return false;
}
//這里其實(shí)就是網(wǎng)上說的客戶端禁用Cookie
if (hreq.isRequestedSessionIdFromCookie()) {
return false;
}
// Is URL encoding permitted
// servlet3.0后可以在項(xiàng)目web.xml里關(guān)掉URL重寫襟雷,對(duì)應(yīng)tomat7之后
if (!hreq.getServletContext().getEffectiveSessionTrackingModes().
contains(SessionTrackingMode.URL)) {
return false;
}
if (SecurityUtil.isPackageProtectionEnabled()) {
return (
AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
@Override
public Boolean run(){
return Boolean.valueOf(doIsEncodeable(hreq, session, location));
}
})).booleanValue();
} else {
//這個(gè)方法會(huì)重寫URL
return doIsEncodeable(hreq, session, location);
}
}
其中調(diào)用Request
對(duì)象的isRequestedSessionIdFromCookie
判斷客戶端Cookie是否可用,里面邏輯也很簡(jiǎn)單仁烹,就是讀取request里面有沒有傳JSESSIONID這個(gè)Cookie耸弄。所以網(wǎng)上有些人說第一次訪問,其實(shí)只要客戶端沒有傳JSESSIONID卓缰,tomcat都假定Cookie不可用