is it possible validate keycloak token on a header different than Authorization?

Viewed 315

I am implementing keycloak and so far works very nice. I did a basic configuration based on the documentation that looks sort of like this:

@Primary
public class AuthConfig extends KeycloakWebSecurityConfigurerAdapter {

@Primary
@Bean
public KeycloakConfigResolver KeycloakConfigResolver() {
    return new KeycloakSpringBootConfigResolver();
}

public SimpleAuthorityMapper grantedAuthority() {
    SimpleAuthorityMapper mapper = new SimpleAuthorityMapper();
    return mapper;
}

@Primary
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) {
    KeycloakAuthenticationProvider keycloakAuthenticationProvider = keycloakAuthenticationProvider();
    keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(grantedAuthority());
    auth.authenticationProvider(keycloakAuthenticationProvider);
}

@Override
protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
    return new NullAuthenticatedSessionStrategy();
}

@Primary
@Bean
public AuthenticationProvider authenticationProvider() {
    return keycloakAuthenticationProvider();
}


@Primary
@Bean
public FilterRegistrationBean keycloakPreAuthActionsFilterRegistrationBean(
  KeycloakPreAuthActionsFilter filter) {
    FilterRegistrationBean registrationBean = new FilterRegistrationBean(filter);
    return registrationBean;
}

@Primary
@Bean
public FilterRegistrationBean keycloakAuthenticatedActionsFilterBean(
  KeycloakAuthenticatedActionsFilter filter) {
    FilterRegistrationBean registrationBean = new FilterRegistrationBean(filter);
    registrationBean.setEnabled(false);
    return registrationBean;
}

@Primary
@Bean
public FilterRegistrationBean keycloakSecurityContextRequestFilterBean(
  KeycloakSecurityContextRequestFilter filter) {
    FilterRegistrationBean registrationBean = new FilterRegistrationBean(filter);
    return registrationBean;
}

@Primary
@Bean
@Override
@ConditionalOnMissingBean(HttpSessionManager.class)
protected HttpSessionManager httpSessionManager() {
    return new HttpSessionManager();
}

might have duplicated configuration but the point is that works fine when authenticating api to api tokens (thats why I set it to create no session) and when authenticating user tokens ...

So, when I send a user token I can do something like this:

public foo getUser(Principal userPrincipal) {
...
}

and then I will be able to access the user owner of the token and get info from the JWT token.

My question is, all this happens when sending the header Authentication and at this moment and I need to send a different header than Authentication when validating the user so I can receive API token and user token and decide (in based of JWT info and other logic) which token I will use to later call another service.

Is it possible to create my own Keycloak "session" for my user token and then create the UserPrincipal from that?

Any idea?

0 Answers
Related