Migrating ServletRequestListener after adopting spring-sessions

239 views Asked by At

We recently started integrating spring-session in our project. This is a SOA based application and have some logic implementations to manage sessions.

  1. HttpSessionListener which we use to log out the sessions in case of timeout and monitored through sessionDestroyed(HttpSessionEvent httpSessionEvent).
  2. ServletRequestListener which we use to manage HTTP session invalidations. This is implemented in requestDestroyed(ServletRequestEvent servletRequestEvent) method.

For item #1, I found an article here, which describes the use of SessionEventHttpSessionListenerAdapter as supported by Spring Session. And have this configuration added in my session.xml.

<context:annotation-config/>
<bean class="org.springframework.session.hazelcast.config.annotation.web.http.HazelcastHttpSessionConfiguration"/>

<bean class="org.springframework.session.web.http.SessionEventHttpSessionListenerAdapter">
    <constructor-arg>
        <list>
            <bean class="xxx.xxx.xxx.xxx.xxx.xxx.MyHttpSessionListener"/>
        </list>
    </constructor-arg>
</bean>

Also added below configuration in my deployment descriptor (e.g. web.xml)

<filter>
    <filter-name>springSessionRepositoryFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSessionRepositoryFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>ERROR</dispatcher>
</filter-mapping>

After some debugging, I have noticed that my HttpSessionListener is using the session provided by spring session.

However, for item #2, using ServletRequestListener implementation, I could not find a way to migrate it to spring session. It is still using the session provided by the container.

Is there a corresponding adapter, like SessionEventHttpSessionListenerAdapter, intended for ServletRequestListener? Is this supported by Spring Session? What other options do I have to get our functionality working with spring session?

1

There are 1 answers

1
nbats On

I ended up using custom Filter to manage HTTP session invalidation. Modified the web.xml with the following entries in sequence:

(a) Register springSessionRepositoryFilter

<filter>
    <filter-name>springSessionRepositoryFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSessionRepositoryFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

(b) Add requestContextFilter

<filter>
    <filter-name>requestContextFilter</filter-name>
    <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>requestContextFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

(c) Add custom filter

<filter>
    <filter-name>myCustomServletRequestFilter</filter-name>
    <filter-class>com.xxx.xxx.xxx.servlet.filter.MyCustomServletRequestFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>myCustomServletRequestFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

(d) Load spring session context

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:application-context.xml</param-value>
</context-param>

All HTTP requests are now using spring session.