Spring boot 带Hazelcast和Tomcat的弹簧靴

Spring boot 带Hazelcast和Tomcat的弹簧靴,spring-boot,hazelcast,embedded-tomcat-8,Spring Boot,Hazelcast,Embedded Tomcat 8,如何使用Hazelcast作为一个http会话存储区,其中嵌入了具有Spring引导和Spring安全性的Tomcat?我看到有一个嵌入式ServletContainerCustomizer和SpringAwareWebFilter,但我不知道如何使用它 As,您需要配置Hazelcast的SpringAwareWebFilter和SessionListener。在Spring Boot中,可以通过分别声明一个FilterRegistrationBean和一个ServletListenerReg

如何使用Hazelcast作为一个http会话存储区,其中嵌入了具有Spring引导和Spring安全性的Tomcat?我看到有一个嵌入式ServletContainerCustomizer和SpringAwareWebFilter,但我不知道如何使用它

As,您需要配置Hazelcast的
SpringAwareWebFilter
SessionListener
。在Spring Boot中,可以通过分别声明一个
FilterRegistrationBean
和一个
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());
}

为什么不使用Spring会话?这很容易

我们实际上是在Redis中持久化这些值,而不是使用Tomcat的HttpSession。Spring会话将HttpSession替换为Redis支持的实现。当Spring Security的SecurityContextPersistenceFilter将SecurityContext保存到HttpSession时,它将被持久化到Redis中

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
@EnableRedisHttpSession 
public class HttpSessionConfig {
}


#src/main/resources/application.properties
spring.redis.host=localhost
spring.redis.password=secret
spring.redis.port=6379