Spring Session 不适用于 Sitemesh
Posted
技术标签:
【中文标题】Spring Session 不适用于 Sitemesh【英文标题】:Spring Session not working with Sitemesh 【发布时间】:2017-05-24 08:32:47 【问题描述】:我正在努力在遗留应用程序中实现 Spring Session。我按照 Spring 文档实现了会话库,一切似乎都运行良好,除了......
通过sitemesh在每个页面上装饰一个UI元素。此 UI 元素检查会话中的对象以查看它是否应该显示。当通过 sitemesh 装饰器从会话中检索对象时,该对象为空。当我通过控制器或主要内容 JSP 访问同一个对象时,该对象被填充并且一切正常。
换句话说:
Web request -> tomcat -> controller -> jsp -> jstl -> session object = ok
然后它继续渲染sitemesh decorator adds main.jsp -> jstl -> same session object is null
我认为这一定是配置问题。我可以确认,当我注释掉 springSessionRepositoryFilter
时,页面会正确呈现,但是我没有使用 spring 会话并访问我的 redis 服务器。如果有人有任何想法,我将不胜感激。谢谢!我的 web.xml 看起来像这样。
<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>
【问题讨论】:
在sitemesh 装饰器中,你如何获得对会话对象的引用?能否提供代码示例? 我没有看到任何配置告诉 sitemesh 过滤器在哪里寻找会话对象。弹簧安全过滤器上发生了很多棘手的事情。我认为它可能正在创建自己的会话对象,然后随后出现的 sitemesh 过滤器不知道在哪里看。这听起来有道理吗? 你的装饰器jsp看起来像什么试图访问会话中的对象(并发现它为空)? @user1521567 你曾经解决过这个问题吗?我遇到了同样的问题。 @PaulReiners 是否有可能提供 mvce 示例,可能是一个 github 项目?谢谢 【参考方案1】:也许这会有所帮助,您在哪里使用它:
<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>
尝试用这个替换它:
<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>
我在这两个帖子中查阅了这些信息:
Link 1
Link 2
【讨论】:
以上是关于Spring Session 不适用于 Sitemesh的主要内容,如果未能解决你的问题,请参考以下文章
expo-auth-session/providers/google 不适用于独立应用程序:invalid_scheme
Spring @RefreshScope 不适用于 @Component