22

我正在使用以下技术开发一个项目:

  • 春天
  • ShiroFilter
  • 漂亮的面孔
  • Tomcat服务器

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

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

4

7 回答 7

37

对于 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>
于 2012-07-04T10:56:24.340 回答
7

以下过滤器可能会解决您的问题(来自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() {
    }
}
于 2012-07-04T11:04:41.880 回答
3
  • Tomcat 6,在您的 context.xml 中添加 disableURLRewriting="true"

  • Tomcat 7 和 ServletFilter 已经讨论过了

  • 或以编程方式:

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

于 2013-03-12T07:18:59.147 回答
3

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

<http auto-config="false" disable-url-rewriting="true">
于 2013-06-03T21:40:18.887 回答
2

正如其他人所建议的那样,您将希望将其从 Tomcat 中取出,但如果您还没有设置 cookie,Shiro 将其附加到重定向的末尾仍然会有问题。关于这个问题有两张未解决的票:

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

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

我试图让 Tuckey 的 URL Re-write 工作并在一段时间后取得了部分成功。问题是 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,如果禁用,他们将无法登录。

于 2014-11-19T21:37:56.230 回答
1

If you are not using Servlet 3.0, you can also achieve this using a PrettyFaces Rewrite Rule: http://ocpsoft.org/support/topic/url-rewrite-removing-the-jsessionid-from-the-url#post-410

于 2012-07-04T14:57:51.543 回答
1

码头Webapp上下文:

Set<SessionTrackingMode> trackingModes = new HashSet<>();
trackingModes.add(SessionTrackingMode.COOKIE);
context.getSessionHandler().getSessionManager().setSessionTrackingModes(trackingModes);
于 2017-08-14T10:01:06.377 回答