2009-06-07 47 views

回答

63

您可以禁用只使用此過濾器的搜索引擎,但我建議使用它的所有響應,因爲它比只是不友好的搜索引擎更糟糕。它暴露了可用於某些安全漏洞的會話ID(more info)。

的Tomcat 6(預6.0.30)

可以使用tuckey rewrite filter

Example config爲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在上下文配置禁用此行爲。

的Tomcat 7和Tomcat 8

Tomcat 7 onwards您可以添加在會話配置以下。

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

爲什麼使用重寫時你可以不創建會話cookie? – 2012-01-18 22:50:19

+1

請查看這個答案的日期。 Tomcat 7和跟蹤模式功能在2009年不可用。現在按照版本信息更新。 – Pool 2012-12-20 15:42:38

+0

Tomcat 6支持上下文元素的'disableURLRewriting'屬性。請參閱http://tomcat.apache.org/tomcat-6.0-doc/config/context.html – 2014-01-07 15:41:10

13

對包裝在一個HttpServletResponseWrapper僅僅返回URL從encodeRedirectUrlencodeRedirectURLencodeUrlencodeURL不變response所有網址中使用Filter。從泳池的回答

5

報價:

可以使用tuckey重寫過濾器。

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

值得一提的是,即使jsessionid不再可見,這仍然允許基於Cookie的會話處理。 (摘自他的另一篇文章:Can I turn off the HttpSession in web.xml?

PS。我沒有足夠的評論聲望,否則我會將此添加到他的帖子中作爲評論。

2

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

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

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

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

乾杯, 馬克

2

默認情況下,Cookie通常是在Tomcat服務器(你可以明確地通過使用Cookie設置= server.xml中的元素真)啓用。啓用cookie意味着jsessionID不會附加到URL,因爲會話將使用cookie進行管理。 但是,即使在啓用cookie後,jsessionID也會附加到第一個請求的URL中,因爲Web服務器在該階段不知道cookie是否已啓用。爲了消除這種jsessionIDs,您可以使用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

19

找到更多這方面的信息,這是可以做到這在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規範。

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

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

4

在Tomcat 6.0中,您可以在您的tomcat安裝的/ config路徑中將disableURLRewriting =「true」用於context.xml中。

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

context.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的輸出它的搜索引擎友好...

享受

相關問題