与Hazelcast和Tomcat的Spring Boot

lla*_*bda 5 hazelcast spring-boot embedded-tomcat-8

如何使用Hazelcast作为带有Spring Boot和Spring Security的嵌入式Tomcat的http会话存储?我看到有一个EmbeddedServletContainerCustomizer和SpringAwareWebFilter,但我不明白如何使用它.

And*_*son 12

Hazelcast的文档中所述,您需要配置Hazelcast SpringAwareWebFilterSessionListener.您可以通过分别声明a FilterRegistrationBean和a来在Spring Boot中执行此操作ServletListenerRegistrationBean:

@Bean
public FilterRegistrationBean hazelcastFilter() {
    FilterRegistrationBean registration = new FilterRegistrationBean(new SpringAwareWebFilter());

    registration.addUrlPatterns("/*");
    registration.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE);

    // Configure init parameters as appropriate:
    // registration.addInitParameter("foo", "bar");

    return registration;
}

@Bean
public ServletListenerRegistrationBean<SessionListener> hazelcastSessionListener() {
    return new ServletListenerRegistrationBean<SessionListener>(new SessionListener());
}
Run Code Online (Sandbox Code Playgroud)

SpringAwareWebFilter并且SessionListener都在Hazelcast的hazelcast-wm模块中,因此您需要添加依赖于com.hazelcast:hazelcast-wmpom.xmlbuild.gradle.hazelcast-wm还要求Spring Security位于类路径上.

现在,当您运行应用程序时,您应该在启动期间看到Hazelcast的日志输出,类似于以下内容:

2014-12-17 10:29:32.401  INFO 94332 --- [ost-startStop-1] com.hazelcast.config.XmlConfigLocator    : Loading 'hazelcast-default.xml' from classpath.
2014-12-17 10:29:32.435  INFO 94332 --- [ost-startStop-1] c.hazelcast.web.HazelcastInstanceLoader  : Creating a new HazelcastInstance for session replication
2014-12-17 10:29:32.582  INFO 94332 --- [ost-startStop-1] c.h.instance.DefaultAddressPicker        : [LOCAL] [dev] [3.3.3] Prefer IPv4 stack is true.
2014-12-17 10:29:32.590  INFO 94332 --- [ost-startStop-1] c.h.instance.DefaultAddressPicker        : [LOCAL] [dev] [3.3.3] Picked Address[169.254.144.237]:5701, using socket ServerSocket[addr=/0:0:0:0:0:0:0:0,localport=5701], bind any local is true
2014-12-17 10:29:32.612  INFO 94332 --- [ost-startStop-1] c.h.spi.impl.BasicOperationScheduler     : [169.254.144.237]:5701 [dev] [3.3.3] Starting with 16 generic operation threads and 16 partition operation threads.
2014-12-17 10:29:32.657  INFO 94332 --- [ost-startStop-1] com.hazelcast.system                     : [169.254.144.237]:5701 [dev] [3.3.3] Hazelcast 3.3.3 (20141112 - eadb69c) starting at Address[169.254.144.237]:5701
2014-12-17 10:29:32.657  INFO 94332 --- [ost-startStop-1] com.hazelcast.system                     : [169.254.144.237]:5701 [dev] [3.3.3] Copyright (C) 2008-2014 Hazelcast.com
2014-12-17 10:29:32.661  INFO 94332 --- [ost-startStop-1] com.hazelcast.instance.Node              : [169.254.144.237]:5701 [dev] [3.3.3] Creating MulticastJoiner
2014-12-17 10:29:32.664  INFO 94332 --- [ost-startStop-1] com.hazelcast.core.LifecycleService      : [169.254.144.237]:5701 [dev] [3.3.3] Address[169.254.144.237]:5701 is STARTING
2014-12-17 10:29:38.482  INFO 94332 --- [ost-startStop-1] com.hazelcast.cluster.MulticastJoiner    : [169.254.144.237]:5701 [dev] [3.3.3] 


Members [1] {
    Member [169.254.144.237]:5701 this
}  

2014-12-17 10:29:38.503  INFO 94332 --- [ost-startStop-1] com.hazelcast.core.LifecycleService      : [169.254.144.237]:5701 [dev] [3.3.3] Address[169.254.144.237]:5701 is STARTED
Run Code Online (Sandbox Code Playgroud)