title: Use keycloak for only authentication and use custom filter for authorization (Spring Boot)
body: <p>I am trying to use keycloak just for authentication and have my own custom filter for authorization. So ideal flow will be: First Keycloak filter authenticates the request and sets authentication object in context. Then my custom filter should run and it should get that existing authentication object, add authorities in that authentication object and set it back in context.</p>
  <p>My securityConfig is extending KeycloakWebSecurityConfigurerAdapter like this</p>
  <pre><code>@Configuration
  @EnableWebSecurity
  class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter
  {
      @Override
      protected void configure(HttpSecurity http) throws Exception
     {
        super.configure(http);
          http
          .cors()
          .and()
          .csrf().ignoringAntMatchers(&quot;/&quot;,&quot;/auth&quot;,&quot;/auth/logout&quot;).csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()).and()
          .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
          .authorizeRequests()
          .antMatchers(
                  &quot;/&quot;,
                  &quot;/auth&quot;,
                  &quot;/password/**&quot;,
                &quot;/register/**&quot;,
                &quot;/v2/api-docs&quot;,
                &quot;/actuator/**&quot;,
                &quot;/configuration/ui&quot;,
                &quot;/swagger-resources&quot;,
                &quot;/configuration/security&quot;,
                &quot;/swagger-ui.html&quot;,
                &quot;/webjars/**&quot;,
                &quot;/swagger-resources/configuration/ui&quot;,
                &quot;/swagger-resources/configuration/security&quot;,
                &quot;/browser/index.html#&quot;,
                &quot;/browser/**&quot;).permitAll()
          .antMatchers(HttpMethod.POST, REGISTER).permitAll()
          .antMatchers(HttpMethod.POST, CONFIRM).permitAll()
          .anyRequest().authenticated()
          .and()
          .addFilter(new JWTAuthorizationFilter(authenticationManager(),context))
  //      .addFilterAfter(new JWTAuthorizationFilter(authenticationManager(),context), KeycloakAuthenticationProcessingFilter.class)
          .headers()
          .contentSecurityPolicy(&quot;script-src 'self'&quot;);
  }
  </code></pre>
  <p>It runs KeycloakAuthenticationProcessingFilter first and then my custom filter(JWTAuthorizationFilter) but then it calls KeycloakAuthenticationProcessingFilter agains due to which authentication object is set again and authorities are cleared. (i tried couple of things. current code plus commented line and a few more)</p>
  <p>So first of all is it the right way to use keycloak in speing boot application if so then how can i make it work that my filter runs last in the filter chain?</p>