简体   繁体   English

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

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

Is it possible to turnoff jsessionid in the url in tomcat? tomcat中url中的jsessionid是否可以关闭? the jsessionid seems not too search engine friendly. jsessionid 似乎对搜索引擎不太友好。

You can disable for just search engines using this filter, but I'd advise using it for all responses as it's worse than just search engine unfriendly.您可以仅禁用使用此过滤器的搜索引擎,但我建议将它用于所有响应,因为它比搜索引擎不友好更糟糕。 It exposes the session ID which can be used for certain security exploits ( more info ).它公开了可用于某些安全漏洞的会话 ID( 更多信息)。

Tomcat 6 (pre 6.0.30) Tomcat 6(6.0.30 之前)

You can use the tuckey rewrite filter .您可以使用tuckey 重写过滤器

Example config for Tuckey filter: 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 and onwards) Tomcat 6(6.0.30 及更高版本)

You can use disableURLRewriting in the context configuration to disable this behaviour.您可以在上下文配置中使用disableURLRewriting来禁用此行为。

Tomcat 7 and Tomcat 8雄猫 7 和雄猫 8

From Tomcat 7 onwards you can add the following in the session config.Tomcat 7 开始,您可以在会话配置中添加以下内容。

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

Tomcat 7 and Tomcat 8 support the above config in your web-app web.xml, which disables URL-based sessions. Tomcat 7 和 Tomcat 8 在您的 web-app web.xml 中支持上述配置,这会禁用基于 URL 的会话。

It is possible to do this in Tomcat 6.0 with: disableURLRewriting可以在 Tomcat 6.0 中使用: disableURLRewriting

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

eg例如

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

Within Tomcat 7.0, this is controlled with the following within an application: ServletContext.setSessionTrackingModes()在 Tomcat 7.0 中,这由应用程序中的以下内容控制: ServletContext.setSessionTrackingModes()

Tomcat 7.0 follows the Servlet 3.0 specifications. Tomcat 7.0 遵循 Servlet 3.0 规范。

对所有将response包装在HttpServletResponseWrapper中的 URL 使用FilterencodeRedirectURL返回从encodeRedirectUrlencodeRedirectURLencodeUrlencodeURL不变的 URL。

Quote from Pool's answer:引自 Pool 的回答:

You can use the tuckey rewrite filter.您可以使用 tuckey 重写过滤器。

You can disable for just search engines using this filter, but I'd advise using it for all responses as it's worse than just search engine unfriendly.您可以仅禁用使用此过滤器的搜索引擎,但我建议将它用于所有响应,因为它比搜索引擎不友好更糟糕。 It exposes the session ID which can be used for certain security exploits (more info).它公开了可用于某些安全漏洞的会话 ID(更多信息)。

It's worth mentioning, that this will still allow cookie based session handling even though the jsessionid is not visible anymore.值得一提的是,即使 jsessionid 不再可见,这仍将允许基于 cookie 的会话处理。 (taken from his other post: Can I turn off the HttpSession in web.xml? ) (摘自他的另一篇文章: 我可以关闭 web.xml 中的 HttpSession 吗?

PS.附注。 I don't have enough reputation to comment, otherwise I would have added this to his post above as a comment.我没有足够的声誉发表评论,否则我会将其添加到他上面的帖子中作为评论。

In Tomcat 6.0 you could use disableURLRewriting="true" into context.xml from your /config path of you tomcat instalation.在 Tomcat 6.0 中,您可以将 disableURLRewriting="true" 从您的 tomcat 安装的 /config 路径中​​使用到 context.xml 中。

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

context.xml file上下文.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>

... ...

Now tomcat output it's search engine friendly...现在 tomcat 输出它的搜索引擎友好...

Enjoy享受

Also if you have Apache in front of Tomcat you can strip out the jsession with a mod_rewrite filter.此外,如果您在 Tomcat 前面有 Apache,您可以使用 mod_rewrite 过滤器去除 jsession。

Add the following to your apache config.将以下内容添加到您的 apache 配置中。

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

This will do a 301 redirect to a page without the jsessionid.这将执行 301 重定向到没有 jsessionid 的页面。 Obviously this will completely disable url jsessionid's but this is what I needed.显然,这将完全禁用 url jsessionid,但这正是我所需要的。

Cheers, Mark干杯,马克

By default, cookies are enabled in Tomcat server(you can explicitly set it by using cookies=true in element of server.xml).默认情况下,Tomcat 服务器中启用了 cookie(您可以通过在 server.xml 的元素中使用 cookies=true 来显式设置它)。 Enabling cookies means that jsessionID will not be appended to URL's since session will be managed using cookies.启用 cookie 意味着 jsessionID 将不会附加到 URL 中,因为会话将使用 cookie 进行管理。 However, even after cookies are enabled, jsessionID's are appended to the URL for first request as the webserver doesn't know at that stage if cookies have been enabled.但是,即使启用了 cookie,jsessionID 也会附加到第一个请求的 URL 中,因为 Web 服务器在该阶段不知道是否启用了 cookie。 To remove such jsessionIDs, you can using tuckey rewrite rules:要删除此类 jsessionID,您可以使用 tuckey 重写规则:

You can find more information on this at http://javatechworld.blogspot.com/2011/01/how-to-remove-jsessionid-from-url-java.html您可以在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>

You can find more information on this at http://javatechworld.blogspot.com/2011/01/how-to-remove-jsessionid-from-url-java.html您可以在http://javatechworld.blogspot.com/2011/01/how-to-remove-jsessionid-from-url-java.html上找到更多信息

in tomcat 7 and above, you can add this in tomcat/conf/context.xml在tomcat 7及以上,你可以在tomcat/conf/context.xml中添加这个

<Context cookies="false">

to disable JSESSIONID.禁用 JSESSIONID。 More on this help doc (refer cookies section).有关帮助文档的更多信息(请参阅 cookies 部分)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM