Use keycloak for only authentication and use custom filter for authorization (Spring boot)

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.

My securityConfig is extending KeycloakWebSecurityConfigurerAdapter like this

class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter
    protected void configure(HttpSecurity http) throws Exception
		.antMatchers(HttpMethod.POST, REGISTER).permitAll()
		.antMatchers(HttpMethod.POST, CONFIRM).permitAll()
		.addFilter(new JWTAuthorizationFilter(authenticationManager(),context))
//		.addFilterAfter(new JWTAuthorizationFilter(authenticationManager(),context), KeycloakAuthenticationProcessingFilter.class)
	    .contentSecurityPolicy("script-src 'self'");

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)

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.

1 Like