我使用这些技术开发了一个项目:
a)容器:Glashfish;
b) 弹簧安全;
c) Java 服务器端;
d) MySQL。使用的代码:
@WebListener 公共类 HazelcastInitializer 实现 ServletContextListener {
private HazelcastInstance instance;
@Override
public void contextInitialized(ServletContextEvent sce) {
String sessionMapName = "spring:session:sessions";
ServletContext sc = sce.getServletContext();
ClientConfig clientConfig = new ClientConfig();
clientConfig.getGroupConfig().setName("value").setPassword("value-pass");
clientConfig.getNetworkConfig().addAddress("ip");
clientConfig.getNetworkConfig().setSmartRouting(true);
Collection<SerializerConfig> scfg = new ArrayList<SerializerConfig>();
SerializerConfig serializer = new SerializerConfig()
.setTypeClass(Object.class)
.setImplementation(new ObjectStreamSerializer());
scfg.add(serializer);
clientConfig.getSerializationConfig().setSerializerConfigs(scfg);
instance = HazelcastClient.newHazelcastClient(clientConfig);
Map<String, ExpiringSession> sessions = instance.getMap(sessionMapName);
SessionRepository<ExpiringSession> sessionRepository
= new MapSessionRepository(sessions);
SessionRepositoryFilter<ExpiringSession> filter
= new SessionRepositoryFilter<ExpiringSession>(sessionRepository);
Dynamic fr = sc.addFilter("springSessionFilter", filter);
fr.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
if (instance != null) {
instance.shutdown();
}
}
}
- 我看到这段代码连接到 Hazelcast 服务器但没有持久化会话。我做错了什么?