簡體   English   中英

是否可以在 tomcat servlet 中禁用 jsessionid?

[英]Is it possible to disable jsessionid in tomcat servlet?

tomcat中url中的jsessionid是否可以關閉? jsessionid 似乎對搜索引擎不太友好。

您可以僅禁用使用此過濾器的搜索引擎,但我建議將它用於所有響應,因為它比搜索引擎不友好更糟糕。 它公開了可用於某些安全漏洞的會話 ID( 更多信息)。

Tomcat 6(6.0.30 之前)

您可以使用tuckey 重寫過濾器

Tuckey 過濾器的示例配置

<outbound-rule encodefirst="true">
  <name>Strip URL Session ID's</name>
  <from>^(.*?)(?:\;jsessionid=[^\?#]*)?(\?[^#]*)?(#.*)?$</from>
  <to>$1$2$3</to>
</outbound-rule>

Tomcat 6(6.0.30 及更高版本)

您可以在上下文配置中使用disableURLRewriting來禁用此行為。

雄貓 7 和雄貓 8

Tomcat 7 開始,您可以在會話配置中添加以下內容。

<session-config>
    <tracking-mode>COOKIE</tracking-mode>
</session-config>
 <session-config>
     <tracking-mode>COOKIE</tracking-mode>
 </session-config> 

Tomcat 7 和 Tomcat 8 在您的 web-app web.xml 中支持上述配置,這會禁用基於 URL 的會話。

可以在 Tomcat 6.0 中使用: disableURLRewriting

http://tomcat.apache.org/tomcat-6.0-doc/config/context.html

例如

<?xml version='1.0' encoding='utf-8'?>
<Context docBase="PATH_TO_WEBAPP" path="/CONTEXT" disableURLRewriting="true">
</Context>

在 Tomcat 7.0 中,這由應用程序中的以下內容控制: ServletContext.setSessionTrackingModes()

Tomcat 7.0 遵循 Servlet 3.0 規范。

對所有將response包裝在HttpServletResponseWrapper中的 URL 使用FilterencodeRedirectURL返回從encodeRedirectUrlencodeRedirectURLencodeUrlencodeURL不變的 URL。

引自 Pool 的回答:

您可以使用 tuckey 重寫過濾器。

您可以僅禁用使用此過濾器的搜索引擎,但我建議將它用於所有響應,因為它比搜索引擎不友好更糟糕。 它公開了可用於某些安全漏洞的會話 ID(更多信息)。

值得一提的是,即使 jsessionid 不再可見,這仍將允許基於 cookie 的會話處理。 (摘自他的另一篇文章: 我可以關閉 web.xml 中的 HttpSession 嗎?

附注。 我沒有足夠的聲譽發表評論,否則我會將其添加到他上面的帖子中作為評論。

在 Tomcat 6.0 中,您可以將 disableURLRewriting="true" 從您的 tomcat 安裝的 /config 路徑中​​使用到 context.xml 中。

http://tomcat.apache.org/tomcat-6.0-doc/config/context.html

上下文.xml 文件

<?xml version='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- The contents of this file will be loaded for each web application -->
<Context disableURLRewriting="true">

    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->

    <!-- Uncomment this to enable Comet connection tacking (provides events
         on session expiration as well as webapp lifecycle) -->
    <!--
    <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
    -->

</Context>

...

現在 tomcat 輸出它的搜索引擎友好...

享受

此外,如果您在 Tomcat 前面有 Apache,您可以使用 mod_rewrite 過濾器去除 jsession。

將以下內容添加到您的 apache 配置中。

#Fix up tomcat jsession appending rule issue
RewriteRule  ^/(.*);jsessionid=(.*) /$1 [R=301,L]

這將執行 301 重定向到沒有 jsessionid 的頁面。 顯然,這將完全禁用 url jsessionid,但這正是我所需要的。

干杯,馬克

默認情況下,Tomcat 服務器中啟用了 cookie(您可以通過在 server.xml 的元素中使用 cookies=true 來顯式設置它)。 啟用 cookie 意味着 jsessionID 將不會附加到 URL 中,因為會話將使用 cookie 進行管理。 但是,即使啟用了 cookie,jsessionID 也會附加到第一個請求的 URL 中,因為 Web 服務器在該階段不知道是否啟用了 cookie。 要刪除此類 jsessionID,您可以使用 tuckey 重寫規則:

您可以在http://javatechworld.blogspot.com/2011/01/how-to-remove-jsessionid-from-url-java.html上找到更多信息

<outbound-rule encodefirst="true">
    <note>Remove jsessionid from embedded urls - for urls WITH query parameters</note>
    <from>^/(.*);jsessionid=.*[?](.*)$</from>
    <to encode="false">/$1?$2</to>
</outbound-rule>

<outbound-rule encodefirst="true">
    <note>Remove jsessionid from embedded urls - for urls WITHOUT query parameters</note>
    <from>^/(.*);jsessionid=.*[^?]$</from>
    <to encode="false">/$1</to>
</outbound-rule>

您可以在http://javatechworld.blogspot.com/2011/01/how-to-remove-jsessionid-from-url-java.html上找到更多信息

在tomcat 7及以上,你可以在tomcat/conf/context.xml中添加這個

<Context cookies="false">

禁用 JSESSIONID。 有關幫助文檔的更多信息(請參閱 cookies 部分)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM