Tomcat - Disable JSESSIONID in URL

解决Tomcat6中URL重写导致的JSESSIONID问题
本文讨论了在使用Tomcat6处理Java web应用时遇到的URL重写问题,该问题可能导致JSESSIONID通过GET方法传递,从而影响安全性并可能对SEO造成负面影响。文章提供了多种解决方案来禁用URL重写,包括修改context.xml配置、使用Servlet Filter和升级到Tomcat7。同时强调了确保浏览器支持session cookies的重要性。

source: https://fralef.me/tomcat-disable-jsessionid-in-url.html

 

I had a problem with a Java webapp that works within a Tomcat 6 container.

 

In fact when you block sites from setting any data inside your browser, Tomcat 6 rewrites the URL and add a JSESSIONID parameter in it. URL session IDs are sensible informations that shouldn't be transmitted via GET method for security concerns. It may also have a bad impact on SEO. Because sessionid is unique, multiple visits by the same search bot will return identical content with different URLs.

 

  1. https://webapp.com/index.jsp;jsessionid=557206C363F1267A24AB769CA0DE4529.node01  

 

Security is a major concern for our customers, and JSESSIONIDs appearing in the URLs freak them out (especially when they demonstrate that you can get a URL from the app, email it to someone else, and have that person magically bypass authentication and assume the role of the other user - of course as long as the session is still valid).

 

The thing is that URL-based session tracking is intended for web clients that do not support session cookies. Every browser worth mentioning supports these cookies, and almost nobody surfs with them disabled. Moreover we are comfortable saying that in order to use our application you need to have cookies enabled, so I'm making the assumption that if we disable the feature of putting JSESSIONID into the URLs cookie-based session setting/tracking will still function just as we expect it.

 

You have multiple solutions to disable URL rewriting :

 

1. 'disableURLRewriting' attribute

In Tomcat 6, you can disable URL rewriting by setting 'disableURLRewriting' attribute to true in your context.xml.

  1. <?xml version='1.0' encoding='utf-8'?>  

  2. <Context docBase="PATH_TO_WEBAPP" path="/CONTEXT" disableURLRewriting="true">  

  3. </Context>  

For this you have to make sure that attribute "cookies" in not set to false. This is the default.

 

Attribute cookies

Set to true if you want cookies to be used for session identifier communication if supported by the client (this is the default). Set to false if you want to disable the use of cookies for session identifier communication, and rely only on URL rewriting by the application.

Attribute disableURLRewriting

Set to true to disable support for using URL rewriting to track session IDs for clients of this Context. URL rewriting is an optional component of the servlet 2.5 specification but disabling URL rewriting will result in non-compliant behaviour since the specification requires that there must be a way to retain sessions if the client doesn't allow session cookies. If not specified, the specification compliant default value of false will be used.

 

2. "Servlet Filter"

You can use a servlet filter such as Tuckey which allow you to rewrite URLs before they get to your code.

 

3. Switch to Tomcat 7 !

The Servlet 3.0 standard gives you two ways to disable URL session rewriting. This works in Tomcat 7, Glassfish v3, and any other Servlet 3.0-compliant servlet container. First, you can add this to your web.xml webapp config:

 

  1. <session-config>  

  2.      <tracking-mode>COOKIE</tracking-mode>  

  3. </session-config>  

Or programmatically, you can use:

 

  1. servletContext.setSessionTrackingModes(EnumSet.of(SessionTrackingMode.COOKIE));  


转载于:https://my.oschina.net/sunpr/blog/340965

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值