Spring Session not working with Sitemesh
Asked Answered
B

1

8

I'm working on implementing Spring Session in a legacy application. I followed the Spring documentation to get the session library implemented and everything seems to be working well, except....

There is a UI element that is decorated on every page through sitemesh. This UI element checks an object in the session to see if it should be displayed or not. When the object is retrieved from the session through the sitemesh decorator, the object is null. When I access the same object through the controller or main content JSP, the object is populated and everything works fine.

In other words:

Web request -> tomcat -> controller -> jsp -> jstl -> session object = ok

then it continues rendering sitemesh decorator adds main.jsp -> jstl -> same session object is null

I think this must be a configuration issue. I can confirm that when I comment out the springSessionRepositoryFilter the page renders correctly, but then I am not using spring session and hitting my redis server. If anyone has any thoughts, I would really appreciate it. Thanks! My web.xml looks like this.

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0" id="WebApp_1398187215147">
    <display-name>MyApplication Web Application</display-name>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
    </listener>

    <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>

    <filter>
        <description>spring-security-filter</description>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>
        <description>sitemesh-filter</description>
        <filter-name>MyApplicationSitemeshFilter</filter-name>
        <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>MyApplicationSitemeshFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <servlet>
        <description>MyApplication-spring-servlet</description>
        <servlet-name>MyApplicationSpringServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:MyApplication-web-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>MyApplicationSpringServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>MyApplicationSpringServlet</servlet-name>
        <url-pattern>/dwr/*</url-pattern>
    </servlet-mapping>

    <servlet>
        <description>MyApplication-webflow-servlet</description>
        <servlet-name>MyApplicationWebFlowServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>WEB-INF/config/MyApplication-webflow-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>MyApplicationWebFlowServlet</servlet-name>
        <url-pattern>/manageRegistration</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>MyApplicationWebFlowServlet</servlet-name>
        <url-pattern>/forgotUserCredentials</url-pattern>
    </servlet-mapping>


    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <error-page>
        <error-code>404</error-code>
        <location>/WEB-INF/pages/error/error_redirect.jsp</location>
    </error-page>

    <error-page>
        <exception-type>java.lang.Exception</exception-type>
        <location>/WEB-INF/pages/error/error_redirect.jsp</location>
    </error-page>
    <error-page>
        <error-code>500</error-code>
        <location>/WEB-INF/pages/error/error_redirect.jsp</location>
    </error-page>
    <error-page>
        <exception-type>java.io.IOException</exception-type>
        <location>/WEB-INF/pages/error/error_redirect.jsp</location>
    </error-page>

</web-app>
Bernardinebernardo answered 9/1, 2017 at 23:46 Comment(5)
In the sitemesh decorator, how are you getting a reference to the session object? Can you provide a code sample?Liqueur
I don't see any configuration that tells the sitemesh filter where to look for the session object. There's a lot of gnarly stuff happening on the spring security filter. I think it may be creating its own session object, and then the sitemesh filter which comes afterward doesn't know where to look. Does that sound plausible?Bernardinebernardo
What does your decorator jsp look like that is trying to access the object in session (and finds that it is null)?Liqueur
@Bernardinebernardo Were you ever able to solve this? I'm running into the same problem.Escolar
@PaulReiners Would it be possible to provide mvce example possibly a github project ? ThanksCyme
W
0

Maybe this can help, where you are using this:

<filter>
    <description>sitemesh-filter</description>
    <filter-name>MyApplicationSitemeshFilter</filter-name>
    <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>MyApplicationSitemeshFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Try replacing it with this:

   <filter>
        <filter-name>sitemesh</filter-name>
        <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>sitemesh</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>ERROR</dispatcher>
    </filter-mapping>

I consult this information in these two posts:

Link 1

Link 2

Woundwort answered 17/2, 2020 at 18:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.