Search code examples
springjdbcspring-session

After using Filter of spring-session-jdbc session object is empty in later CompositeFilter


Problem Describe(seems timing problem):

After using SpringSessionRepositoryFilter, session object is empty during the processing period of OtherFilter at the begining of every request

What I have tried:

  • In the Controller and JSP after OtherFilter, session object is not empty and works fine
  • Without using springSessionRepositoryFilter, session object is notempty and works fine in OtherFilter

The configuratioin is like below:

<bean class="org.springframework.web.filter.CompositeFilter" name="springChainFilter">
    <property name="filters">
        <list>
            <bean id="springSessionRepositoryFilter" class="org.springframework.session.web.http.SessionRepositoryFilter">
            </bean>

            <!--Other Later Filter -->
            <bean id="otherFilter" class="xxx.xxx.OtherFilter">
            </bean>
        </list>
    </property>
</bean>

<bean class="org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration"/>

<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <constructor-arg ref="dataSource"/>
</bean>

<bean id="cookieSerializer" class="org.springframework.session.web.http.DefaultCookieSerializer">
    <property name="cookieName" value="JSESSIONID" />
</bean>

OtherFilter Defination is like below:

public class OtherFilter extends OncePerRequestFilter {


    @Autowired
    private SessionObj sessionObj;

    ......
}

Session Object Defination is like below:

@Component
@SessionScope
public class SessionObj implements Serializable {

    private static final long serialVersionUID = 1L;


    private String xxId;

    ......
}

Environment Version Info:

  • spring-session-jdbc-2.1.5.RELEASE
  • wildfly-11.0.0.Final
  • Oracle Database 18c Express Edition Release 18.0.0.0.0

Solution

  • Another Solution for this Question

    Add the requestContextFilter between springSessionRepositoryFilter and otherFilter

    <bean class="org.springframework.web.filter.CompositeFilter" name="springChainFilter">
        <property name="filters">
            <list>
                <bean id="springSessionRepositoryFilter" class="org.springframework.session.web.http.SessionRepositoryFilter">
                </bean>
                <!--Request Context Filter -->
                <bean id="requestContextFilter" class="org.springframework.web.filter.RequestContextFilter" />
                <!--Other Later Filter -->
                <bean id="otherFilter" class="xxx.xxx.OtherFilter">
                </bean>
            </list>
        </property>
    </bean>
    

    In RequestContextFilter(applied by spring framework)

    • the wrappedRequest is exposed to the current thread, through both LocaleContextHolder and RequestContextHolder
    • the [requestAttributes] is bound with an instance of SessionRepositoryRequestWrapper (do thing like FrameworkSevlet do in future)