title: FindByIndexNameSessionRepository “RedisConnectionFactory is required” configuration error
body: <p>Implementing the logic of limiting active sessions for each user (number of possible sessions stored in the database, sessions in Redis), I came across the problem of configuring <code>FindByIndexNameSessionRepository</code>.</p>
  
  <p>Due to the fact that I am injecting <code>FindByIndexNameSessionRepository</code> spring starts to configure the bean in the <a href="https://docs.spring.io/spring-session/docs/current/api/org/springframework/session/data/redis/config/annotation/web/http/RedisHttpSessionConfiguration.html" rel="nofollow noreferrer">RedisHttpSessionConfiguration</a> class, and the fields are not injected into the class via setters.</p>
  
  <p>As a result, I get the error:</p>
  
  <pre><code>Caused by: java.lang.IllegalStateException: RedisConnectionFactory is required
  at org.springframework.util.Assert.state(Assert.java:73) ~[spring-core-5.2.5.RELEASE.jar:5.2.5.RELEASE]
  at org.springframework.data.redis.core.RedisAccessor.afterPropertiesSet(RedisAccessor.java:38) ~[spring-data-redis-2.2.6.RELEASE.jar:2.2.6.RELEASE]
  at org.springframework.data.redis.core.RedisTemplate.afterPropertiesSet(RedisTemplate.java:127) ~[spring-data-redis-2.2.6.RELEASE.jar:2.2.6.RELEASE]
  at org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration.createRedisTemplate(RedisHttpSessionConfiguration.java:291) ~[spring-session-data-redis-2.2.2.RELEASE.jar:2.2.2.RELEASE]
  at org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration.sessionRepository(RedisHttpSessionConfiguration.java:120) ~[spring-session-data-redis-2.2.2.RELEASE.jar:2.2.2.RELEASE]
  at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
  at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
  at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
  at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
  at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
  </code></pre>
  
  <p>Config code:</p>
  
  <pre><code>@EnableRedisHttpSession
  @EnableWebSecurity
  @Configuration
  @RequiredArgsConstructor
  public class CustomConfiguration extends WebSecurityConfigurerAdapter {
      private final FindByIndexNameSessionRepository sessionRepository;    
      // ....
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http
              .authorizeRequests()
                  .antMatchers("/**")
                  .authenticated()
                  .and()
              .formLogin()
                  .and()
              // ....
              .sessionManagement()
                  .sessionAuthenticationStrategy(new CustomAuthenticationStrategy(sessionRegistry(), sessionsLimitRepository));
      }
  
      @Bean
      public SpringSessionBackedSessionRegistry sessionRegistry() {
          return new SpringSessionBackedSessionRegistry(sessionRepository);
      }
  
      @Bean
      public LettuceConnectionFactory redisConnectionFactory() {
          return new LettuceConnectionFactory(new RedisStandaloneConfiguration("server", 6379));
      }
  
      @Bean
      public HttpSessionEventPublisher httpSessionEventPublisher() {
          return new HttpSessionEventPublisher();
     }
  
  }
  </code></pre>
  
  <p>pom.xml</p>
  
  <pre><code>&lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-security&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-data-redis&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.session&lt;/groupId&gt;
          &lt;artifactId&gt;spring-session-data-redis&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.session&lt;/groupId&gt;
          &lt;artifactId&gt;spring-session-core&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
      &lt;!-- .... --&gt;
  &lt;/dependencies&gt;
  </code></pre>
  
  <p>If you remove\comment out the places where <code>FindByIndexNameSessionRepository</code> is used, then the <a href="https://docs.spring.io/spring-session/docs/current/api/org/springframework/session/data/redis/config/annotation/web/http/RedisHttpSessionConfiguration.html" rel="nofollow noreferrer">RedisHttpSessionConfiguration</a> setters are called by the "guts" of the spring when the <code>springSessionRepositoryFilter</code> bean is created.</p>
  
  <p>If I understood correctly, then I accidentally broke the order of configuring the beans. In attempts to fix it, I tried @DependsOn, @Lazy and various frauds with moving bins to different classes of configs, changing the order of loading configs, and so on.</p>
  
  <p>UPD: Add bean <code>httpSessionEventPublisher</code> and pom.xml</p>