从URL中删除jsessionid

时间:2012-07-04 10:51:57

标签: spring tomcat6 shiro prettyfaces

我正在使用以下技术开展项目:

  • 弹簧
  • ShiroFilter
  • PrettyFaces
  • Tomcat服务器

当我在tomcat服务器上部署它时,我在URL的末尾添加了"JSESSIONID 456jghd787aa"

我试图解决这个问题,但我无法做到这一点。

7 个答案:

答案 0 :(得分:35)

对于tomcat 7,将其添加到web.xml

<session-config>
  <!-- Disables URL-based sessions (no more 'jsessionid' in the URL using Tomcat) -->
  <tracking-mode>COOKIE</tracking-mode>
</session-config>

答案 1 :(得分:6)

以下过滤器可以解决您的问题(来自http://randomcoder.org/maven/site/randomcoder-website/cobertura/org.randomcoder.security.DisableUrlSessionFilter.html

package com.companyname.projectname.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import javax.servlet.http.HttpSession;

/**
 * Servlet filter which disables URL-encoded session identifiers.
 * 
 * <pre>
 * Copyright (c) 2006, Craig Condit. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *   * Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *     
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * </pre>
 */
public class DisableUrlSessionFilter implements Filter {

/*    private static Log logger = LogFactory.getLog(DisableUrlSessionFilter.class);
*/
    /**
     * Filters requests to disable URL-based session identifiers.
     */
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        // skip non-http requests
        if (!(request instanceof HttpServletRequest)) {
            chain.doFilter(request, response);
            return;
        }

        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;

        // clear session if session id in URL
        if (httpRequest.isRequestedSessionIdFromURL()) {
            HttpSession session = httpRequest.getSession();
            if (session != null) {
                session.invalidate();
            }
        }

        // wrap response to remove URL encoding
        HttpServletResponseWrapper wrappedResponse = new HttpServletResponseWrapper(
                httpResponse) {
            @Override
            public String encodeRedirectUrl(String url) {
                return url;
            }

            @Override
            public String encodeRedirectURL(String url) {
                return url;
            }

            @Override
            public String encodeUrl(String url) {
                return url;
            }

            @Override
            public String encodeURL(String url) {
                return url;
            }
        };

        // process next request in chain
        chain.doFilter(request, wrappedResponse);
    }

    /**
     * Unused.
     */
    public void init(FilterConfig config) throws ServletException {
    }

    /**
     * Unused.
     */
    public void destroy() {
    }
}

答案 2 :(得分:3)

  • Tomcat 6,在context.xml中添加disableURLRewriting =“true”

  • 已经讨论过Tomcat 7和ServletFilter

  • 或以编程方式:

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

答案 3 :(得分:3)

您可以将这些设置添加到http标记中,如下所示:

<http auto-config="false" disable-url-rewriting="true">

答案 4 :(得分:2)

你想要把它从Tomcat中拿出来,正如其他人所建议的那样,但是如果你没有设置cookie,那么你仍然会遇到Shiro在重定向时将其附加到最后的问题然而。这个问题有两张打开的门票:

https://issues.apache.org/jira/browse/SHIRO-360

https://issues.apache.org/jira/browse/SHIRO-361

我试图让Tuckey的URL重写工作,并在一段时间后取得了部分成功。问题是Shiro没有调用response.encodeURL(),因此会触发出站规则。我能够使用以下两个规则重定向入站请求以删除会话ID:

<rule>
    <note>Remove jsessionid from embedded urls - for urls WITH query parameters</note>
    <from>^/(.*);JSESSIONID=.*[?](.*)$</from>
    <to type="redirect">/$1?$2</to>
</rule>

<rule>
    <note>Remove jsessionid from embedded urls - for urls WITHOUT query parameters</note>
    <from>^/(.*);JSESSIONID=.*[^?]$</from>
    <to type="redirect">/$1</to>
</rule>

这至少使它不会显示在浏览器中,但它并没有完全解决问题,因为会话ID是在URL上发送的,并且重定向到没有它的位置。如果它永远不会显示出来会更好。

<强>更新

SHIRO-360和SHIRO-361已修复,修复程序在Shiro 1.3.0中。根据SHIRO-361的Brian Demers的说法:

  

设置sessionManager.sessionIdUrlRewritingEnabled = false以禁用将JSESSIONID附加到URL。

     

注意:如果用户已禁用cookie,则如果禁用,则无法登录。

答案 5 :(得分:1)

如果您不使用Servlet 3.0,您还可以使用PrettyFaces重写规则来实现此目的:http://ocpsoft.org/support/topic/url-rewrite-removing-the-jsessionid-from-the-url#post-410

答案 6 :(得分:1)

Jetty WebappContext:

Set<SessionTrackingMode> trackingModes = new HashSet<>();
trackingModes.add(SessionTrackingMode.COOKIE);
context.getSessionHandler().getSessionManager().setSessionTrackingModes(trackingModes);