java 如何在java中实现自定义http会话?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7135096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:46:13  来源:igfitidea点击:

how to implement custom http session in java?

javahttpsession

提问by plymouth5

I will need to implement my own version of HttpSession in Java. I have found very little information which explains how achieve such a feat.

我需要在 Java 中实现我自己的 HttpSession 版本。我发现很少有信息可以解释如何实现这样的壮举。

I guess my problem is - how do I override the existing HttpSession no matter the application server's implementation?

我想我的问题是 - 无论应用服务器的实现如何,我如何覆盖现有的 HttpSession ?

I did run across a quality but rather old read which helps me achieve my goal - http://java.sun.com/developer/technicalArticles/Servlets/ServletControl/

我确实遇到了一本质量很好但很老的书,这有助于我实现我的目标 - http://java.sun.com/developer/technicalArticles/Servlets/ServletControl/

Are there any other approaches?

还有其他方法吗?

回答by Dani

Its two ways.

它的两种方式。

"Wrapping" the original HttpSessionin your own HttpServletRequestWrapperimplementation.

HttpSession在您自己的HttpServletRequestWrapper实现中“包装”原件。

I made this a short time ago for clustering distributed sessions with Hazelcast and Spring Session.

我不久前做了这个,用于使用 Hazelcast 和 Spring Session 对分布式会话进行集群。

Hereis explained pretty well.

这里解释得很好。

First, implement your own HttpServletRequestWrapper

首先,实现自己的 HttpServletRequestWrapper

public class SessionRepositoryRequestWrapper extends HttpServletRequestWrapper {

        public SessionRepositoryRequestWrapper(HttpServletRequest original) {
                super(original);
        }

        public HttpSession getSession() {
                return getSession(true);
        }

        public HttpSession getSession(boolean createNew) {
                // create an HttpSession implementation from Spring Session
        }

        // ... other methods delegate to the original HttpServletRequest ...
}

After, from your own Filter, wraps the original HttpSession, and put it inside the FilterChainprovided by your Servlet Container.

之后,从您自己的 Filter 中,将原始 包装起来HttpSession,并将其放入FilterChain您的 Servlet Container 提供的 中。

public class SessionRepositoryFilter implements Filter {

        public doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
                HttpServletRequest httpRequest = (HttpServletRequest) request;
                SessionRepositoryRequestWrapper customRequest =
                        new SessionRepositoryRequestWrapper(httpRequest);

                chain.doFilter(customRequest, response, chain);
        }

        // ...
}

Finally, set your Filter at the beginning in the web.xml to ensure it performs before any other.

最后,在 web.xml 的开头设置您的过滤器,以确保它在任何其他过滤器之前执行。

The second manner to achieve it is providing to your Servlet Container your custom SessionManager.

实现它的第二种方式是向您的 Servlet 容器提供您的自定义 SessionManager。

For example, in Tomcat 7.

例如,在Tomcat 7 中

回答by CrackerHyman9

Create a new class, and implement HttpSession:

创建一个新类,并实现 HttpSession:

public class MyHttpSession implements javax.servlet.http.HttpSession {

    // and implement all the methods

}

Disclaimer: I have not tested this myself:

免责声明:我自己没有测试过:

Then write a filter with a url-pattern of /* and extend HttpServletRequestWrapper. Your wrapper should return your custom HttpSessionclass in getSession(boolean). In the filter, use your own HttpServletRequestWrapper.

然后用 /* 和扩展的 url-pattern 编写一个过滤器HttpServletRequestWrapper。您的包装器应该HttpSessiongetSession(boolean). 在过滤器中,使用您自己的HttpServletRequestWrapper.

回答by llambda

It doesn't seem like it's easily doable in a portable fashion that would work among different containers, since the HttpSession implementation is provided by the J2EE container.

以可在不同容器之间工作的可移植方式似乎并不容易,因为 HttpSession 实现是由 J2EE 容器提供的。

However, to achieve a similar result, you can implement a javax.servlet.Filter and javax.servlet.HttpSessionListener and in your filter, wrap the ServletRequest and ServletResponse, such as described in Spring Boot with Hazelcast and Tomcat.

但是,要获得类似的结果,您可以实现 javax.servlet.Filter 和 javax.servlet.HttpSessionListener 并在您的过滤器中包装 ServletRequest 和 ServletResponse,如Spring Boot with Hazelcast 和 Tomcat 中所述