如何在调用HttpServletResponse.encodeURL()时将Tomcat配置为不将会话ID编码到URL中

看起来像一个愚蠢的问题,答案是“不要使用encodeURL()!” 但是我正在使用在JSP中使用netui锚标签的代码库,我需要禁用将JSESSIONID写入URL,因为这是一个安全风险。

在WebLogic中,您可以通过在weblogic.xml中配置url-rewriting-enabled来配置它(我知道,因为我在WebLogic服务器中编写了该function!)。 但是,我找不到Tomcat的等效配置选项。

想到没有设置。 但是这很容易通过创建一个第一个条目Filter监听感兴趣的url-pattern (可能是/* ?)并用HttpServletResponseWrapper实现替换ServletResponse ,其中encodeURL()返回未经修改的相同参数。

开球示例:

 public void doFilter(ServletRequest request, ServletResponse response) throws ServletException, IOException { chain.doFilter(request, new HttpServletResponseWrapper((HttpServletResponse) response) { public String encodeURL(String url) { return url; } }); } 

Tomcat 6支持disableURLRewriting属性,可以在Context元素中设置为true

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