その2つの方法。
HttpSession
独自のHttpServletRequestWrapper
実装でオリジナルを「ラッピング」します。
Hazelcast と Spring Session を使用して分散セッションをクラスタリングするために、これを少し前に作成しました。
ここはかなりうまく説明されています。
まずは自分で実装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 ...
}
その後、独自のフィルターから元の をラップし、サーブレット コンテナーによって提供されるHttpSession
内に配置します。FilterChain
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);
}
// ...
}
最後に、web.xml の先頭にフィルタを設定して、他のフィルタよりも先に実行されるようにします。
それを実現する 2 番目の方法は、サーブレット コンテナにカスタム SessionManager を提供することです。
たとえば、Tomcat 7では。