Java Spring会话中具有HeaderHttpSessionStrategy的Null HttpSessionManager
spring会话允许在同一浏览器中进行多个会话,我遇到了一个问题。我使用MapSessionRepository来存储会话和CookieHttpSessionStrategy。通过这种配置,一切正常 我被要求将CookieHttpSessionStrategy更改为HeaderHttpSessionStrategy。现在,当我试图从请求中获取HttpSessionManager时,我得到一个NullPointerException 这是我以前的配置(我正在工作): 这是我的新配置:Java Spring会话中具有HeaderHttpSessionStrategy的Null HttpSessionManager,java,spring,session,spring-session,Java,Spring,Session,Spring Session,spring会话允许在同一浏览器中进行多个会话,我遇到了一个问题。我使用MapSessionRepository来存储会话和CookieHttpSessionStrategy。通过这种配置,一切正常 我被要求将CookieHttpSessionStrategy更改为HeaderHttpSessionStrategy。现在,当我试图从请求中获取HttpSessionManager时,我得到一个NullPointerException 这是我以前的配置(我正在工作): 这是我的新配置: @Confi
@Configuration
@ComponentScan(basePackages = { "com.company.name" }, excludeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = { "com.company.name.web*" }))
@EnableScheduling
@EnableAspectJAutoProxy
@EnableCaching
@EnableSpringHttpSession
public class Config {
....
....
@Bean
public MapSessionRepository sessionRepository() {
return new MapSessionRepository();
}
@Bean
public HttpSessionStrategy httpSessionStrategy() {
return new HeaderHttpSessionStrategy();
}
}
我通过以下方式获取SessionManager对象:
//Se obtiene el manager de sesiones de spring session
HttpSessionManager sessionManager = (HttpSessionManager) httpRequest.getAttribute(HttpSessionManager.class.getName());
当我使用头策略时,请求中没有org.springframework.session.web.http.HttpSessionManager属性。我做错什么了吗?我想我可以改变策略,只需返回HeaderHttpSessionStrategy对象或实现新的自定义策略
谢谢你的帮助 您可能需要查看
SessionRepositoryFilter
类的源代码,以了解会话管理器为空的原因。您将看到它默认使用CookieHttpSessionStrategy
。CookieHttpSessionStrategy
有一个wrapRequest
方法,用于设置请求的sessionManager属性
如果将任何其他策略注入会话筛选器,则它将使用一个不会设置会话管理器的MultiHttpSessionStrategyAdapter
静态类。这就是为什么您将此设置为null
编辑:
您可以通过获取bean引用来获取会话管理器,如下所示:
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(httpRequest.getServletContext());
HttpSessionManager sessionManager = ctx.getBean(HttpSessionManager.class);
本质上,您正在获取您在配置中定义的“httpSessionStrategy”bean的引用。您可能需要查看
SessionRepositoryFilter
类的源代码,以了解会话管理器为空的原因。您将看到它默认使用CookieHttpSessionStrategy
。CookieHttpSessionStrategy
有一个wrapRequest
方法,用于设置请求的sessionManager属性
如果将任何其他策略注入会话筛选器,则它将使用一个不会设置会话管理器的MultiHttpSessionStrategyAdapter
静态类。这就是为什么您将此设置为null
编辑:
您可以通过获取bean引用来获取会话管理器,如下所示:
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(httpRequest.getServletContext());
HttpSessionManager sessionManager = ctx.getBean(HttpSessionManager.class);
本质上,您正在获取您在配置中定义的“httpSessionStrategy”bean的引用。为什么您还要获取
HttpSessionManager
?如果您需要它,只需注入它……为什么您还要像那样获得HttpSessionManager
?如果您需要它,只需注入它……事实证明,Spring会话不支持使用HeaderHttpSessionStrategy的多个会话。我使用了HttpSessionStrategy,结果表明Spring会话不支持使用HeaderHttpSessionStrategy的多个会话。我改用了HttpSessionStrategy