具有Hazelcast和Tomcat的Spring Boot [英] Spring Boot with Hazelcast and Tomcat
问题描述
如何将Hazelcast用作带有Spring Boot和Spring Security的嵌入式Tomcat的http会话存储?我看到有一个EmbeddedServletContainerCustomizer和SpringAwareWebFilter,但我不知道如何使用它.
How do you use Hazelcast as a http session store with embedded Tomcat with Spring Boot and Spring Security? I see there is a EmbeddedServletContainerCustomizer and SpringAwareWebFilter but I don't understand how to use it.
推荐答案
如Hazelcast文档中所述的 ,则需要配置Hazelcast的SpringAwareWebFilter
和SessionListener
.您可以在Spring Boot中通过分别声明FilterRegistrationBean
和ServletListenerRegistrationBean
来做到这一点:
As described in Hazelcast's documentation, you need to configure Hazelcast's SpringAwareWebFilter
and SessionListener
. You can do so in Spring Boot by declaring a FilterRegistrationBean
and a ServletListenerRegistrationBean
respectively:
@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());
}
SpringAwareWebFilter
和SessionListener
都在Hazelcast的hazelcast-wm
模块中,因此您需要在pom.xml
或build.gradle
中添加对com.hazelcast:hazelcast-wm
的依赖. hazelcast-wm
还要求Spring Security必须在类路径上.
SpringAwareWebFilter
and SessionListener
are both in Hazelcast's hazelcast-wm
module so you'll need to add a dependency on com.hazelcast:hazelcast-wm
to your pom.xml
or build.gradle
. hazelcast-wm
also requires Spring Security to be on the classpath.
现在,当您运行应用程序时,应该在启动过程中看到Hazelcast的日志输出,类似于以下内容:
Now, when you run your application, you should see log output from Hazelcast during startup that's similar to the following:
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
这篇关于具有Hazelcast和Tomcat的Spring Boot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!