是否有可能在tomcat servlet中禁用jsessionid?

是否有可能在tomcat中closuresurl中的jsessionid? jsessionid似乎不太适合search引擎。

你可以只使用这个filter的search引擎禁用,但我build议使用它的所有反应,因为它不仅仅是不友好的search引擎。 它暴露了可用于某些安全漏洞的会话ID( 更多信息 )。

雄猫6(6.0.30之前)

你可以使用tuckey重写filter 。

Tuckeyfilter的configuration示例 :

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

雄猫6(6.0.30及以上)

您可以在上下文configuration中使用disableURLRewriting来禁用此行为。

Tomcat 7和Tomcat 8

从Tomcat 7开始,您可以在会话configuration中添加以下内容。

 <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中支持上述configuration,这会禁用基于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规范。

在包装HttpServletResponseWrapper response所有URL上使用Filter ,该response只是简单地从encodeRedirectUrlencodeRedirectURLencodeUrlencodeURL返回未更改的URL。

引用来自Pool的答案:

你可以使用tuckey重写filter。

你可以只使用这个filter的search引擎禁用,但我build议使用它的所有反应,因为它不仅仅是不友好的search引擎。 它暴露了可用于某些安全漏洞的会话ID(更多信息)。

值得一提的是,即使jsessionid不再可见,这仍然允许基于cookie的会话处理。 (取自他的另一篇文章: 我可以closuresweb.xml中的HttpSession? )

PS。 我没有足够的评价声望,否则我会在上面添加这个作为评论。

在Tomcat 6.0中,你可以在tomcat安装的/ configpath中使用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输出它的search引擎友好…

请享用

另外,如果您在Tomcat前面有Apache,则可以使用mod_rewritefilter去除jsession。

将以下内容添加到您的apacheconfiguration中。

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

这将做一个301redirect到一个没有jsessionid的页面。 显然这将完全禁用url jsessionid的,但这是我所需要的。

干杯,马克

默认情况下,在Tomcat服务器中启用cookie(您可以通过在server.xml元素中使用cookies = true来显式设置它)。 启用cookie意味着jsessionID不会被附加到URL,因为会话将使用cookie进行pipe理。 但是,即使启用cookie后,jsessionID也会附加到第一个请求的URL中,因为Web服务器在该阶段不知道cookie是否已启用。 要删除这样的jsessionID,你可以使用tuckey重写规则:

你可以在http://javatechworld.blogspot.com/2011/01/how-to-remove-jsessionid-from-url-java.htmlfind更多的信息。;

 <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.htmlfind更多的信息。;