2009-06-07 94 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的输出它的搜索引擎友好...

享受

相关问题