title: Unable to use Keycloak in Spring Boot 2.1 due to duplicated Bean Registration httpSessionManager
body: <p>I want to secure my Spring Boot 2.1 app with Keycloak 4.5.</p>
  
  <p>Currently I cannot start the application due to the following error:</p>
  
  <pre><code>Exception encountered during context initialization - cancelling refresh attempt: 
  org.springframework.beans.factory.support.BeanDefinitionOverrideException: 
    Invalid bean definition with name 'httpSessionManager' defined in class path resource [dummy/service/SecurityConfig.class]: 
      Cannot register bean definition [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=securityConfig; factoryMethodName=httpSessionManager; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [dummy/SecurityConfig.class]] for bean 'httpSessionManager': 
  There is already [Generic bean: class [org.keycloak.adapters.springsecurity.management.HttpSessionManager]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in URL [jar:file:/.m2/repository/org/keycloak/keycloak-spring-security-adapter/4.5.0.Final/keycloak-spring-security-adapter-4.5.0.Final.jar!/org/keycloak/adapters/springsecurity/management/HttpSessionManager.class]] bound.
  </code></pre>
  
  <p>My class SecurityConfig (see below) extends from KeycloakWebSecurityConfigurerAdapter. This adapter already defines the bean httpSessionManager.</p>
  
  <p>I understand why this is a problem. Question is, how can I prevent this or fix my conflict? </p>
  
  <p>The Steps I have done so far:</p>
  
  <ul>
  <li>Built my pom (see below) using:
  
  <ul>
  <li>spring-boot-starter-web</li>
  <li>spring-boot-starter-security</li>
  <li>keycloak-spring-boot-starter</li>
  <li>keycloak-adapter-bom in dependencyManagement</li>
  </ul></li>
  <li>Defined an own SecurityConfig extending KeycloakWebSecurityConfigurerAdapter</li>
  </ul>
  
  <p><strong>pom.xml</strong></p>
  
  <pre class="lang-xml prettyprint-override"><code>...
  &lt;parent&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
      &lt;version&gt;2.1.0.RELEASE&lt;/version&gt;
  &lt;/parent&gt;
  
  &lt;properties&gt;
      &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
      &lt;project.reporting.outputEncoding&gt;UTF-8&lt;/project.reporting.outputEncoding&gt;
  
      &lt;java.version&gt;11&lt;/java.version&gt;
      &lt;maven.compiler.source&gt;${java.version}&lt;/maven.compiler.source&gt;
      &lt;maven.compiler.target&gt;${java.version}&lt;/maven.compiler.target&gt;
  
      &lt;keycloak.version&gt;4.5.0.Final&lt;/keycloak.version&gt;
  &lt;/properties&gt;
  
  &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.keycloak&lt;/groupId&gt;
          &lt;artifactId&gt;keycloak-spring-boot-starter&lt;/artifactId&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  
  &lt;dependencyManagement&gt;
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.keycloak.bom&lt;/groupId&gt;
              &lt;artifactId&gt;keycloak-adapter-bom&lt;/artifactId&gt;
              &lt;version&gt;${keycloak.version}&lt;/version&gt;
              &lt;type&gt;pom&lt;/type&gt;
              &lt;scope&gt;import&lt;/scope&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  &lt;/dependencyManagement&gt;
  ...
  </code></pre>
  
  <p><strong>SecurityConfig.java</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>@KeycloakConfiguration
  @EnableGlobalMethodSecurity(prePostEnabled = true)
  @Import(KeycloakWebSecurityConfigurerAdapter.class)
  class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter {
  
      @Autowired
      public void configureGlobal(AuthenticationManagerBuilder auth) {
          KeycloakAuthenticationProvider keycloakAuthenticationProvider = keycloakAuthenticationProvider();
          keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(new SimpleAuthorityMapper());
          auth.authenticationProvider(keycloakAuthenticationProvider);
      }
  
      @Bean
      @Override
      protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
          return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl());
      }
  
      @Bean
      public KeycloakConfigResolver keycloakConfigResolver() {
          return new KeycloakSpringBootConfigResolver();
      }
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          super.configure(http);
          http.csrf().ignoringAntMatchers("/**/*");
          http.authorizeRequests()
                  .anyRequest().permitAll();
      }
  }
  </code></pre>
  
  <p><strong>Update</strong>
  There is a known issue (<a href="https://issues.jboss.org/browse/KEYCLOAK-8725?focusedCommentId=13660313&amp;page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-13660313" rel="noreferrer">KEYCLOAK-8725</a>). The fix is planned for 5.x of Keycloak. However, there was a workaround in the comments. Just replace the annotation @KeyCloakConfiguration with:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Configuration
  @ComponentScan(
      basePackageClasses = KeycloakSecurityComponents.class,
      excludeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.keycloak.adapters.springsecurity.management.HttpSessionManager"))
  @EnableWebSecurity
  </code></pre>