小编典典

具有Hazelcast和Tomcat的Spring Boot

spring-boot

如何将Hazelcast用作带有Spring Boot和Spring
Security的嵌入式Tomcat的http会话存储?我看到有一个EmbeddedServletContainerCustomizer和SpringAwareWebFilter,但是我不知道如何使用它。


阅读 436

收藏
2020-05-30

共1个答案

小编典典

Hazelcast的文档中所述,您需要配置Hazelcast的SpringAwareWebFilterSessionListener。您可以在Spring
Boot中通过分别声明a FilterRegistrationBean和a
来做到这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());
}

SpringAwareWebFilter并且SessionListener都在Hazelcast的hazelcast- wm模块,所以你需要在添加一个依赖com.hazelcast:hazelcast- wm于你pom.xmlbuild.gradlehazelcast-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
2020-05-30